1. 程式人生 > >重溫IO流讀寫檔案

重溫IO流讀寫檔案

前段時間寫後臺跑批每天生成一個增量檔案,今天有空將IO讀寫檔案重溫一下,複製一個文字檔案內容到另外一個文字檔案中;

程式碼如下:使用了5種方式

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * ----------字元流-----------
 * Reader 介面
 * Writer 介面
 * 
 * FileReader
 * FileWriter	不調close不調flush寫入失敗,內容為空;調close不調flush可以寫入成功,但安全起見(停電等情況)flush寫上為好			
 * 
 * BufferedReader
 * BufferedWriter		不調close不調flush寫入失敗,內容為空;調close不調flush可以寫入成功,但安全起見(停電等情況)flush寫上為好
 * 
 * InputStreamReader  輸入轉換流  可以指定讀取的編碼
 * OutputStreamWriter 輸出轉換流  可以指定寫的編碼   	不調close不調flush寫入失敗,內容為空;調close不調flush可以寫入成功,但安全起見(停電等情況)flush寫上為好
 * -----------位元組流---------
 * InputStream 	介面
 * OutputStream 介面
 * 
 * FileInputStream
 * FileOutputStream   這個不close不flush都可以寫入成功
 * 
 * BufferedInputStream
 * BufferedOutputStream   不調close不調flush寫入失敗,內容為空;調close不調flush可以寫入成功,但安全起見(停電等情況)flush寫上為好
 * -------------------
 * 複製文字檔案
 */
public class IOTest1 {
	public static void main(String[] args) {
		copyFile();
	}
	
	/**
	 * 複製文字檔案
	 * 只有 FileWriter丟擲IOException,其他都拋FileNotFoundExcepiton;read、close等方法都拋IOException
	 */
	public static void copyFile() {
		//普通位元組流
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		try{
			fileInputStream = new FileInputStream(new File("d:\\0.txt"));//FileInputStream拋FileNotFoundExcepiton
			fileOutputStream = new FileOutputStream(new File("d:\\1.txt"));//FileOutputStream拋FileNotFoundException
			
			byte[] dd = new byte[1024];
			int len = -1;
			while ((len=fileInputStream.read(dd))!=-1) {
				fileOutputStream.write(dd,0,len);
//				fileOutputStream.flush();//這裡可以不要
			}
			
		}catch (FileNotFoundException e) {
		}catch (IOException e) {
		}finally {
			if(fileOutputStream!=null) {
				try {
					fileOutputStream.close();
				}catch (IOException e) {
				}
			}
			if(fileInputStream!=null) {
				try {
					fileInputStream.close();
				}catch (IOException e) {
				}
			}
		}
		
		//高效位元組流
		BufferedInputStream bufferedInputStream = null;
		BufferedOutputStream bufferedOutputStream = null;
		try {
			bufferedInputStream = new BufferedInputStream(new FileInputStream("d:\\0.txt"));
			bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("d:\\2.txt"));
			
			byte[] dd = new byte[1024];
			int len = -1;
			while ((len=bufferedInputStream.read(dd))!=-1) {
				bufferedOutputStream.write(dd,0,len);
				bufferedOutputStream.flush();//呼叫close方法內部呼叫flush重新整理,但是為了確保資料完整性,還是寫上
			}
		} catch (FileNotFoundException e) {
			// TODO: handle exception
		} catch (IOException e) {
			// TODO: handle exception
		} finally {
			if(bufferedOutputStream!=null) {
				try {
					bufferedOutputStream.close();
				}catch (IOException e) {
				}
			}
			if(bufferedInputStream!=null) {
				try {
					bufferedInputStream.close();
				}catch (IOException e) {
				}
			}
		}
		
		//普通字元流
		FileReader fileReader = null;
		FileWriter fileWriter = null;
		try {
			fileReader = new FileReader("d:\\0.txt");
			fileWriter = new FileWriter("d:\\3.txt");
			
			char[] dd = new char[1024];
			int len = -1;
			while ((len=fileReader.read(dd))!=-1) {
				fileWriter.write(dd,0,len);
				fileWriter.flush();//呼叫close方法內部呼叫flush重新整理,但是為了確保資料完整性,還是寫上
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
		}finally {
			if(fileWriter!=null) {
				try {
					fileWriter.close();
				} catch (IOException e2) {
				}
			}
			
			if(fileReader!=null) {
				try {
					fileReader.close();
				} catch (IOException e2) {
				}
			}
		}
		
		//高效字元流
		BufferedReader bufferedReader = null;
		BufferedWriter bufferedWriter = null;
		try {
			bufferedReader = new BufferedReader(new FileReader("d:\\0.txt"));//FileReader丟擲FileNotFoundException
			bufferedWriter = new BufferedWriter(new FileWriter("d:\\4.txt"));//FileWriter丟擲IOException
			
			String readLine = null;
			while((readLine = bufferedReader.readLine())!=null) {
				bufferedWriter.write(readLine);
				bufferedWriter.newLine();//每寫一行就需要換行
				bufferedWriter.flush();//呼叫close方法內部呼叫flush重新整理,但是為了確保資料完整性,還是寫上
			}
			
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(bufferedWriter!=null) {
				try {
					bufferedWriter.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(bufferedReader!=null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		//轉換流
		InputStreamReader inputStreamReader = null;
		OutputStreamWriter outputStreamWriter = null;
		try {
			inputStreamReader = new InputStreamReader(new FileInputStream("d:\\0.txt"));//FileInputStream拋FileNotFoundException
			outputStreamWriter = new OutputStreamWriter(new FileOutputStream("d:\\5.txt"));//FileOutputStream拋FileNotFoundException
			char[] dd = new char[1024];
			int len = -1;
			while ((len=inputStreamReader.read(dd))!=-1) {
				outputStreamWriter.write(dd,0,len);
				outputStreamWriter.flush();//呼叫close方法內部呼叫flush重新整理,但是為了確保資料完整性,還是寫上
			}
		} catch (FileNotFoundException e) {
			// TODO: handle exception
		} catch (IOException e) {
			// TODO: handle exception
		} finally {
			if(outputStreamWriter!=null) {
				try {
					outputStreamWriter.close();
				}catch (IOException e) {
				}
			}
			if(inputStreamReader!=null) {
				try {
					inputStreamReader.close();
				}catch (IOException e) {
				}
			}
		}
	}
}

程式碼中有詳細註釋,令有同學可能對flush和close有點混淆,可以看
關於java中輸出流flush()方法
結束,希望對看到的朋友有所幫助