IO流的分類:
1、根據流的數據對象來分:
高端流:所有的內存中的流都是高端流,比如:InputStreamReader
低端流:所有的外界設備中的流都是低端流,比如InputStream,OutputStream
如何區分:所有的流對象的後綴中包含Reader或者Writer的都是高端流,反之,則基本上爲低端流,不過也有例外,比如PrintStream就是高端流
2、根據數據的流向來分:
輸出流:是用來寫數據的,是由程序(內存)--->外界設備
輸入流:是用來讀數據的,是由外界設備--->程序(內存)
如何區分:一般來說輸入流帶有Input,輸出流帶有Output
3、根據流數據的格式來分:
字節流:處理聲音或者圖片等二進制的數據的流,比如InputStream
字符流:處理文本數據(如txt文件)的流,比如InputStreamReader
如何區分:可用高低端流來區分,所有的低端流都是字節流,所有的高端流都是字符流
4、根據流數據的包裝過程來分:
原始流:在實例化流的對象的過程中,不需要傳入另外一個流作爲自己構造方法的參數的流,稱之爲原始流。
包裝流:在實例化流的對象的過程中,需要傳入另外一個流作爲自己構造方法發參數的流,稱之爲包裝流。
如何區分:所以的低端流都是原始流,所以的高端流都是包裝流
IO流對象的繼承關係(如下圖):
下面來看一些具體的代碼例子:
按字節來讀取文件
public class ReadFromFile { /** * 以字節爲單位讀取文件,常用於讀二進制文件,如圖片、聲音、影像等文件。 */ public static void readFileByBytes(String fileName) { File file = new File(fileName); InputStream in = null; try { System.out.println("以字節爲單位讀取文件內容,一次讀一個字節:"); // 一次讀一個字節 in = new FileInputStream(file); int tempbyte; while ((tempbyte = in.read()) != -1) { System.out.print(tempbyte); } in.close(); } catch (IOException e) { e.printStackTrace(); return; } try { System.out.println("以字節爲單位讀取文件內容,一次讀多個字節:"); // 一次讀多個字節 byte[] tempbytes = new byte[100]; int byteread = 0; in = new FileInputStream(fileName); ReadFromFile.showAvailableBytes(in); // 讀入多個字節到字節數組中,byteread爲一次讀入的字節數 while ((byteread = in.read(tempbytes)) != -1) { System.out.print(tempbytes, 0, byteread); } } catch (Exception e1) { e1.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e1) { } } } }
按字符來讀取文件
/** * 以字符爲單位讀取文件,常用於讀文本,數字等類型的文件 */ public static void readFileByChars(String fileName) { File file = new File(fileName); Reader reader = null; try { System.out.println("以字符爲單位讀取文件內容,一次讀一個字符:"); // 一次讀一個字符 reader = new InputStreamReader(new FileInputStream(file)); int tempchar; while ((tempchar = reader.read()) != -1) { // 對於windows下,\r\n這兩個字符在一起時,表示一個換行。 // 但如果這兩個字符分開顯示時,會換兩次行。 // 因此,屏蔽掉\r,或者屏蔽\n。否則,將會多出很多空行。 if (((char) tempchar) != '\r') { System.out.print((char) tempchar); } } reader.close(); } catch (Exception e) { e.printStackTrace(); } try { System.out.println("以字符爲單位讀取文件內容,一次讀多個字符:"); // 一次讀多個字符 char[] tempchars = new char[30]; int charread = 0; //由於要以字符來讀取,所以需要套上字符流 reader = new InputStreamReader(new FileInputStream(fileName)); // 讀入多個字符到字符數組中,charread爲一次讀取字符數 while ((charread = reader.read(tempchars)) != -1) { // 同樣屏蔽掉\r不顯示 if ((charread == tempchars.length) && (tempchars[tempchars.length - 1] != '\r')) { System.out.print(tempchars); } else { for (int i = 0; i < charread; i++) { if (tempchars[i] == '\r') { continue; } else { System.out.print(tempchars[i]); } } } } } catch (Exception e1) { e1.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } }
按行來讀取文件
/** * 以行爲單位讀取文件,常用於讀面向行的格式化文件 */ public static void readFileByLines(String fileName) { File file = new File(fileName); BufferedReader reader = null; try { System.out.println("以行爲單位讀取文件內容,一次讀一整行:"); reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; // 一次讀入一行,直到讀入null爲文件結束 while ((tempString = reader.readLine()) != null) { // 顯示行號 System.out.println("line " + line + ": " + tempString); line++; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } }
將一個文件的內容寫入另一個文件(按行來寫)
public class FileTest { public static void main(String[] args) { File file=new File("c:\\test.txt"); BufferedReader read=null; BufferedWriter writer=null; try { writer=new BufferedWriter(new FileWriter("c:\\zwm.txt")); } catch (IOException e1) { e1.printStackTrace(); } try { read=new BufferedReader(new FileReader(file)); String tempString = null; while((tempString=read.readLine())!=null){ writer.append(tempString); writer.newLine();//換行 writer.flush();//需要及時清掉流的緩衝區,萬一文件過大就有可能無法寫入了 } read.close(); writer.close(); System.out.println("文件寫入完成..."); } catch (IOException e) { e.printStackTrace(); } } }