Java I/O流

概念

流:流動,流向 從一端移動到另一端 源頭和目的地
程序 與 文件|數組|網絡連接|數據庫,以程序爲中心。

I/O流分類

  1. 流向:輸入流和輸出流
  2. 數據:
    • 字節流:二進制,可以一切文件,包括純文本,doc,音頻,視頻等
    • 字符流:文本文件,只能處理純文本,txt,html等
  3. 功能:
    • 節點流:包裹源頭
    • 處理流:增強功能,提供性能

字節流和字符流與文件(重點)

  1. 字節流
    • 輸入流:InputStream read(byte[] b),read(byte[] b, int off, int len) + close()
      • FileInputStream
    • 輸出流:OutputStream write(byte[] b),write(byte[] b, int off, int len) + flush() + close()
      • FileOutputStream
  2. 字符流
    • 輸入流:Reader read(char[] cbuf),read(char[] cbuf, int off, int len) + close()
      • FileReader()
    • 輸出流:Writer write(char[] cbuf),write(char[] cbuf, int off, int len) + flush() + close()
      • FileWriter()

操作

  1. 建立聯繫
  2. 選擇流
  3. 操作,數組大小 + read, write
  4. 釋放資源
  • 讀取文件
    1. 建立聯繫:File對象 源頭
    2. 選擇流:文件輸入流 InputStream FileInputStream
    3. 操作:byte[] b = new byte[1024]; + read + 讀取大小 輸出
    4. 釋放資源:關閉
  • 寫出文件
    1. 建立聯繫:File對象 目的地
    2. 選擇流:文件輸出流 OutputStream FileOutputStream
    3. 操作:write() + flush
    4. 釋放資源:關閉
  • 拷貝文件 程序爲橋樑
    1. 建立聯繫 File對象 源頭 目的地
    2. 選擇流:
      • 文件輸入流 InputStream FileInputStream
      • 文件輸出流 OutputStream FileOutputStream
    3. 操作:拷貝
      • byte[] flush = new byte[1024];
      • int len = 0;
      • while(-1!=(len=輸入流.read(flush))) {
        • 輸出流.write(flush, 0, len);
      • }
      • 輸出流.flush
    4. 釋放資源:關閉 兩個流

實例

  1. 讀取文件
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    
    public class Demo01 {
    
    	public static void main(String[] args) {
    		//1.建立聯繫File對象
    		File src = new File("D:/test/1.txt");
    		//2.選擇流
    		InputStream is = null; //提升作用域
    		try {
    			is = new FileInputStream(src); 
    			//3.操作 不斷讀取 緩衝數組
    			byte[] b = new byte[1024];
    			int len; //接收實際讀取大小
    			//循環讀取
    			while(-1!=(len=is.read(b))) {
    				//輸出 字節數組轉字符串
    				String info = new String(b, 0, len);
    				System.out.println(info);
    			}
    		} catch (FileNotFoundException e) {
    			System.out.println("文件不存在!");
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    			System.out.println("讀取文件失敗!");
    		}finally {
    			//4.釋放資源
    			if(null!=is) {
    				try {
    					is.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    					System.out.println("關閉文件輸入流失敗!");
    				}
    			}
    		}
    	}
    }
    

  2. 寫出文件
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    
    public class Demo02 {
    
    	public static void main(String[] args) {
    		// 1.建立聯繫 File對象 目的地
    		File dest = new File("D:/test/2.txt");
    		// 2.選擇流 文件輸出流 OutputStream FileOutputStream
    		OutputStream os = null;
    		// 以追加的形式寫出文件
    		try {
    			os = new FileOutputStream(dest, true);
    			String str = "THE CHO3EN 1";
    			// 3.操作
    			byte[] data = str.getBytes();
    			// 字符串轉字節數組
    			os.write(data, 0, data.length);
    			// 強制刷新出去
    			os.flush();
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    			System.out.println("文件未找到!");
    		} catch (IOException e) {
    			e.printStackTrace();
    			System.out.println("文件寫入失敗!");
    		} finally {
    			// 釋放資源:關閉
    			if (null != os) {
    				try {
    					os.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    					System.out.println("關閉輸出流失敗!");
    				}
    			}
    		}
    	}
    }
    

  3. 拷貝文件
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    public class CopyFileDemo {
    
    	public static void main(String[] args) throws IOException {
    
    		// 1.建立聯繫 源(存在且爲文件)+ 目的地(文件可以不存在)
    		File src = new File("D:/test/1.txt");
    		File dest = new File("D:/test/3.txt");
    		// 2.選擇流
    		InputStream is = new FileInputStream(src);
    		OutputStream os = new FileOutputStream(dest);
    		// 3.文件拷貝 循環 + 讀取 + 寫出
    		byte[] flush = new byte[1024];
    		int len = 0;
    		// 讀取
    		while (-1 != (len = is.read(flush))) {
    			// 寫出
    			os.write(flush, 0, len);
    		}
    		// 強制刷出
    		os.flush();
    		// 4.關閉流
    		os.close();
    		is.close();
    	}
    }

處理流

  • 處理流:增強功能,提供性能,節點流之上
    • 緩衝流
    • 字節緩衝流
      • BufferedInputStream
      • BufferedOutputStream
    • 字符緩衝流
      • BufferedReader  readLin()
      • BufferedWriter     newLine()
  • 轉換流:字節流轉爲字符流 處理亂碼(編碼集,解碼集)
    • 編碼:字符 編碼字符集->二進制
    • 解碼:二進制 解碼字符集->字符
    • 亂碼:編碼與解碼的字符集不統一|字節缺少,長度丟失

實例

  1. 文件拷貝(字節處理流)
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    public class BufferedByteDemo {
    
    	public static void main(String[] args) throws IOException {
    
    		// 1.建立聯繫 源(存在且爲文件)+ 目的地(文件可以不存在)
    		File src = new File("D:/test/1.txt");
    		File dest = new File("D:/test/3.txt");
    		// 2.選擇流
    		InputStream is = new BufferedInputStream(new FileInputStream(src));
    		OutputStream os = new BufferedOutputStream(new FileOutputStream(dest));
    		// 3.文件拷貝 循環 + 讀取 + 寫出
    		byte[] flush = new byte[1024];
    		int len = 0;
    		// 讀取
    		while (-1 != (len = is.read(flush))) {
    			// 寫出
    			os.write(flush, 0, len);
    		}
    		// 強制刷出
    		os.flush();
    		// 4.關閉流
    		os.close();
    		is.close();
    	}
    }
  2. 文件拷貝(字符處理流)
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Reader;
    import java.io.Writer;
    
    public class BufferedCharDemo {
    	public static void main(String[] args) throws IOException {
    		File src = new File("D:/test/4.txt");
    		File dest = new File("D:/test/5.txt");
    		
    		BufferedReader br = new BufferedReader(new FileReader(src));
    		BufferedWriter bw = new BufferedWriter(new FileWriter(dest));
    		
    		String msg = null;
    		
    		while(null!=(msg=br.readLine())) {
    			bw.write(msg);
    			bw.newLine();
    		}
    		
    		bw.flush();
    		bw.close();
    		br.close();
    	}
    }
發佈了10 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章