输入/输出流 I/O(input/output)流 输入流(input stream or input object)的指向称作“源” 输出流(output stream or output object)的指向称作“目的地” 程序的“源”和“目的地”可以是文件、键盘、鼠标、内存或显示器窗口
4个abstract classInputStream (字节输入流)OutputStream (字节输出流)Reader (字符输入流)Writer (字符输出流)
文件 构造方法 1 2 File(String filename); File(String directoryPath, String filename);
属性 1 2 3 4 5 6 7 8 9 10 11 public String getName () public boolean canRead () public boolean canWrite () public boolean exits () public long length () public String getAbsolutePath () public String getParent () public boolean isFile () public boolean isDirectory () public boolean isHidden () public long lastModified ()
目录 1 2 3 4 5 6 7 public boolean mkdir () public String[] list()public String[] list(FilenameFilter ff)public File[] listFiles()public File[] listFiles(FilenameFilter ff)
文件的创建与删除 File file = new File("c:\\myletter","letter.txt");
public boolean delete()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 import java.io.*;class FileAccept implements FilenameFilter { String str = null ; FileAccept(String s) { str = "." + s; } public boolean accept (File dir, String name) { return name.endsWith(str); } } public class demo { public static void main (String args[]) { File dir = new File ("D:\\Java\\code\\course\\src\\..." ); FileAccept fileAccept = new FileAccept ("java" ); File[] files = dir.listFiles(fileAccept); for (int i=0 ; i<files.length; i++) { System.out.println(files[i].getName() + ": " + files[i].length()); } boolean flag = false ; if (files.length>0 ) { flag = files[0 ].delete(); } if (flag) { System.out.println(files[0 ].getName() + " has been deleted." ); } } }
运行可执行文件 使用Runtime类声明一个对象 使用静态方法getRuntime()创建这个对象Runtime rt = Runtime.getRuntime();
rt可以调用exec(String command)
方法打开本地机器的可执行文件/执行一个操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import java.io.*; public class Example9_2 { public static void main (String args[]) { try { Runtime rt = Runtime.getRuntime(); File file = new File ("C:\\windows" , "Notepad.exe" ); rt.exec(file.getAbsolutePath()); } catch (Exception e){} } }
使用Scanner解析文件 先把文件的内容全部读入内存后进行解析,处理速度快 ——> 以空间(内存)换时间 使用Scanner类和正则表达式来解析文件 ——> 以时间换空间(内存)
使用默认分隔符标记解析文件 text:TV cost 876 dollar, Computer cost 2398 dollar. The milk cost 98 dollar. The apple cost 198 dollar.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 import java.io.*;import java.util.*;public class demo2 { public static void main (String args[]) { File file = new File ("C:\\Users\\26555\\Desktop\\JAVA\\cost.txt" ); Scanner scanner = null ; int sum=0 ; try { scanner = new Scanner (file); while (scanner.hasNext()){ try { int price = scanner.nextInt(); sum = sum + price; System.out.println(price); } catch (InputMismatchException exp){ String t = scanner.next(); } } System.out.println("Total Cost:" +sum+" dollar" ); } catch (Exception exp){ System.out.println(exp); } } }
使用正则表达式作为分隔标记解析文件 text市话费:176.89元,长途费:187.98元,网络费:928.66元
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import java.io.*;import java.util.*;public class demo3 { public static void main (String args[]) { File file = new File ("C:\\Users\\26555\\Desktop\\JAVA\\communicate.txt" ); Scanner scanner = null ; double sum = 0 ; try { double fare=0 ; scanner = new Scanner (file); scanner.useDelimiter("[^0123456789.]+" ); while (scanner.hasNextDouble()){ fare = scanner.nextDouble(); sum = sum+fare; System.out.println(fare); } System.out.println("Total: " + sum); } catch (Exception exp){ System.out.println(exp); } } }
单词记忆训练 文件字符流 FileReader类 1 2 3 4 5 6 7 FileReader(String name) FileReader(File file) int read () int read (char b[ ]) int read (char b[ ], int off, int len)
FileWriter类 1 2 3 4 5 6 7 8 FileWriter(String name) FileWriter(File file) void write (char b[]) void write (char b[], int off, int len) void write (String str) void write (String str, int off, int len)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import java.io.*; public class Example9_4 { public static void main (String args[]) { File file = new File ("hello.txt" ); char b[] = "深圳大学" .toCharArray(); try { FileWriter output = new FileWriter (file); output.write(b); output.write("脚踏实地!" ); output.close(); FileReader input = new FileReader (file); int n=0 ; while ((n=input.read(b,0 ,2 ))!=-1 ) { String str = new String (b,0 ,n); System.out.println(str); } input.close(); } catch (IOException e){ System.out.println(e); } } }
缓冲流 BufferedReader类 1 2 3 4 5 6 7 BufferedReader(Reader in) FileReader fr = new FileReader ("Student.txt" ); BufferedReader input = new BufferedReader (fr);
BufferedWriter类 1 2 3 4 5 6 7 FileWriter fw = new FileWriter ("hello.txt" ); BufferedWriter output = new BufferedWriter (fw);write(String s) write(String s, int off, int len)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import java.io.*; public class Example9_5 { public static void main (String args[]) { try { FileReader fr = new FileReader ("input.txt" ); BufferedReader input = new BufferedReader (fr); FileWriter fw = new FileWriter ("output.txt" ); BufferedWriter output = new BufferedWriter (fw); String s=null ; int i=0 ; while ((s = input.readLine())!=null ) { i++; output.write(i + ": " + s); output.newLine(); } output.flush(); output.close(); fw.close(); input.close(); fr.close(); } catch (IOException e){ System.out.println(e); } } }
文件字节流 1 2 3 4 5 6 7 8 FileInputStream(String name) FileInputStream(File file) int read (byte b[ ]) ;int read (byte b[ ], int off, int len) ;
FileOutputStream类 1 2 3 4 5 6 7 FileOutputStream(String name) FileOutputStream(File file) public void write (byte b[]) ;public void write (byte b[], int off, int len) ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 import java.io.*; public class Example9_3 { public static void main (String args[]) { File file = new File ("hello.txt" ); byte b[] = "深圳大学" .getBytes(); try { FileOutputStream output = new FileOutputStream (file); output.write(b); output.close(); FileInputStream input = new FileInputStream (file); int n=0 ; while ( (n=input.read(b,0 ,3 ))!=-1 ) { String str = new String (b,0 ,n); System.out.println(str); } } catch (IOException e){ System.out.println(e); } } }
数据流 DataInputStream类和DataOutputStream类 数据输入流 和 数据输出流 构造方法DataInputStream(InputStream is)
DataOutputStream(OutputStream os)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import java.io.*; public class Example9_8 { public static void main (String args[]) { try { FileOutputStream fos = new FileOutputStream ("jerry.dat" ); DataOutputStream output = new DataOutputStream (fos); output.writeInt(100 ); output.writeChars("I am ok" ); } catch (IOException e){} try { FileInputStream fis = new FileInputStream ("jerry.dat" ); DataInputStream input = new DataInputStream (fis); System.out.println(input.readInt()); char c; while ((c=input.readChar())!='\0' ) System.out.print(c); } catch (IOException e){} } }
对象流 ObjectInputStream类和ObjectOutputStream类 对象输入流 和 对象输出流 构造方法ObjectInputStream(InputStream in)
ObjectOutputStream(OutputStream out)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 import java.io.*; class Goods implements Serializable { String name = null ; double unitPrice; Goods(String name, double unitPrice) { this .name=name; this .unitPrice=unitPrice; } public void setUnitPrice (double unitPrice) { this .unitPrice=unitPrice; } public double getUnitPrice () { return unitPrice; } public void setName (String name) { this .name=name; } public String getName () { return name; } } public class Example9_9 { public static void main (String args[]) { Goods TV1 = new Goods ("HaierTV" ,3468 ); try { FileOutputStream fileOut = new FileOutputStream ("a.txt" ); ObjectOutputStream objectOut = new ObjectOutputStream (fileOut); objectOut.writeObject(TV1); FileInputStream fileIn = new FileInputStream ("a.txt" ); ObjectInputStream objectIn = new ObjectInputStream (fileIn); Goods TV2 = (Goods)objectIn.readObject(); TV2.setUnitPrice(8888 ); TV2.setName("GreatWall" ); System.out.printf("\nTv1:%s,%f" ,TV1.getName(),TV1.getUnitPrice()); System.out.printf("\nTv2:%s,%f" ,TV2.getName(),TV2.getUnitPrice()); } catch (Exception event){ System.out.println(event); } } }
序列化和对象克隆 随机读写流 文件锁 数组流 字符串流