Java IO流讀寫文件

 平時寫IO相關代碼機會挺少的,但卻都知道使用BufferedXXXX來讀寫效率高,沒想到裏面還有這麼多陷阱,這兩天突然被其中一個陷阱折騰一下:讀一個文件,然後寫到另外一個文件,前後兩個文件居然不一樣?

     解決這個問題之後,總結了幾個注意點。

 

注意點一:Reader/Writer讀寫二進制文件是有問題的

 

  1. public void copyFile1() {  
  2.         File srcFile = new File("E://atest//atest.txt");  
  3.         File dstFile = new File("E://btest//btest.txt");  
  4.         BufferedReader in = null;  
  5.         BufferedWriter out = null;  
  6.         try {  
  7.             in = new BufferedReader(new FileReader(srcFile));  
  8.             out = new BufferedWriter(new FileWriter(dstFile));  
  9.               
  10.             String line = null;  
  11.             while((line = in.readLine()) != null) {  
  12.                 out.write(line+"/r/n");  
  13.             }  
  14.         }catch (Exception e) {  
  15.             // TODO: handle exception   
  16.             e.printStackTrace();  
  17.         }finally {  
  18.             if(in != null) {  
  19.                 try {  
  20.                     in.close();  
  21.                 }catch (Exception e) {  
  22.                     // TODO: handle exception   
  23.                     e.printStackTrace();  
  24.                 }  
  25.             }  
  26.               
  27.             if(out != null) {  
  28.                 try {  
  29.                     out.close();  
  30.                 }catch (Exception e) {  
  31.                     // TODO: handle exception   
  32.                     e.printStackTrace();  
  33.                 }  
  34.             }  
  35.         }  

上面代碼使用BufferedReader一行一行地讀取一個文件,然後使用BufferedWriter把讀取到的數據寫到另外一個文件中。如果文件是ASCCII形式的,則內容還是能夠正確讀取的。但如果文件是二進制的,則讀寫後的文件與讀寫前是有很大區別的。當然,把上面的readLine()換成read(char[])仍然不能正確讀寫二進制文件的。讀寫二進制文件請接着看下面注意點。

 

注意點二:read(byte[] b, int offset, int length)中的offset不是指全文件的全文,而是字節數組b的偏移量

現在已經知道使用Reader/Writer不能正確讀取二進制文件,這是因爲Reader/Writer是字符流,那就改用字節流ufferedInputStream/BufferedOutputStream,網上搜索到的例子大概是這樣的:

 

  1. public void copyFile() {  
  2.         File srcFile = new File("E://atest//atest.gif");  
  3.         File dstFile = new File("E://atest//btest.gif");  
  4.         BufferedInputStream in = null;  
  5.         BufferedOutputStream out = null;          
  6.         try {  
  7.             in = new BufferedInputStream(new FileInputStream(srcFile));  
  8.             out = new BufferedOutputStream(new FileOutputStream(dstFile));  
  9.               
  10.             byte[] b = new byte[1024];  
  11.             while(in.read(b) != -1) {  
  12.                 out.write(b);  
  13.             }  
  14.         }catch (Exception e) {  
  15.             // TODO: handle exception   
  16.             e.printStackTrace();  
  17.         }finally {  
  18.             if(in != null) {  
  19.                 try {  
  20.                     in.close();  
  21.                 }catch (Exception e) {  
  22.                     // TODO: handle exception   
  23.                     e.printStackTrace();  
  24.                 }  
  25.             }  
  26.             if(out != null) {  
  27.                 try {  
  28.                     out.close();  
  29.                 }catch (Exception e) {  
  30.                     // TODO: handle exception   
  31.                     e.printStackTrace();  
  32.                 }  
  33.             }  
  34.         }  
  35.     }  

每次讀1024字節,然後寫1024字節。這看似挺正確的,但實際寫出來的文件與原文件是不同的。這樣就懷疑可能是讀寫沒有接上,因而把代碼改成下面的形式:

 

  1. byte[] b = new byte[1024];  
  2.             int offset = 0;  
  3.             int length = -1;  
  4.             while((length = in.read(b, offset, 1024)) != -1) {  
  5.                 out.write(b, offset, length);  
  6.                 offset += length;  
  7.             }  

這是誤以爲:先讀一段,寫一段,然後改變偏移量,然後使用新的偏移量再讀一段、寫一段,直到文件讀寫完畢。但這是錯誤的,因爲使用BufferedXXX後,裏面已經實現了這個過程。而read(byte[] b, int offset, int length)中的offset實際指的是把讀到的數據存入到數組b時,從數組的哪個位置(即offset)開始放置數據;同理,write(byte[] b, int offset, int length)就是把b中的數據,從哪個位置(offset)開始寫到文件中。

 

注意點三:使用 length=read (b, 0, 1024)讀數據時,應該使用write(b, 0, length)來寫

第二個注意點中的第一段代碼的做法雖然在網上比較常見,但是有問題的。問題在哪呢?答案是:問題在byte[] b這個數組上。由於二進制文件使用比較工具時,只知道不同、但不能知道哪些不同(是否有更先進的比較工具?)。怎樣確定它的不同呢?方法很簡單:就把二進制文件改成文本文件就能看出結果了(Reader/Writer這種字符流雖然不能正確讀寫二進制文件,但InputStream/OutputStream這些字節流能既能正確讀寫二進制文件,也能正確讀寫文本文件)。由於使用了每次讀1K(1024字節)的方式,所以會看到的結果是:寫後的文件後面多出一段,這一段的長度與原文件大小以及b數組的大小有關。爲了進一步確定是什麼關係,把讀的文件內容改爲"1234567890123",而把b數組的大小改爲10字節,這時結果就出來了:寫後的文件內容變成"12345678901234567890",就是讀了兩遍。多出的內容的根源在這裏:b數組的大小是10字節,而要讀的內容長度是13字節,那就要讀兩次,第一次讀了前10字節,此時b數組內的元素爲前10個字符;再讀第二次時,由於可讀內容只有3個字符,那b數組的內容只有前3個字符被改變了,後面7個字符仍然保持上一次讀取的內容。所以直接採用write(b)的方式,在第二次寫文件時,內容就多寫了一段不是第二次讀取到的內容。

下面是正確的讀寫(即每次讀了多少內容,寫入的是多少內容,而不是寫入整個數組):

 

  1. public void copyFile() {  
  2.         File srcFile = new File("E://atest//atest.txt");  
  3.         File dstFile = new File("E://btest//btest.txt");  
  4.         BufferedInputStream in = null;  
  5.         BufferedOutputStream out = null;  
  6.         try {  
  7.             in = new BufferedInputStream(new FileInputStream(srcFile));  
  8.             out = new BufferedOutputStream(new FileOutputStream(dstFile));  
  9.               
  10.             int len = -1;  
  11.             byte[] b = new byte[10];  
  12.             while((len = in.read(b)) != -1) {  
  13.                 out.write(b, 0, len);  
  14.             }  
  15.         }catch (Exception e) {  
  16.             // TODO: handle exception   
  17.             e.printStackTrace();  
  18.         }finally {  
  19.             if(in != null) {  
  20.                 try {  
  21.                     in.close();  
  22.                 }catch (Exception e) {  
  23.                     // TODO: handle exception   
  24.                     e.printStackTrace();  
  25.                 }  
  26.             }  
  27.             if(out != null) {  
  28.                 try {  
  29.                     out.close();  
  30.                 }catch (Exception e) {  
  31.                     // TODO: handle exception   
  32.                     e.printStackTrace();  
  33.                 }  
  34.             }  
  35.         }  
  36.     }  

 

注意點四:flush()和close()

flush()是把寫緩衝區內的內容全部”吐“到文件上,如果沒有它,就有可能很多內容還存在於寫緩衝區內,而不是在文件中,也就是還有丟失的可能。

close()中會調用flush()。它是文件真正完成的標誌,文件內容寫完成後不關閉文件流,會導致一些”古怪“的問題。這個在網絡中的流更能體現。

所以,寫文件完成後注意關閉文件讀寫流。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章