2010-08-07 56 views
3

我用下面的代碼來寫一個數組到文件中:的Java I/O,writng一個數組到一個文本文件

FileWriter fstream1=new FileWriter("outx.txt"); 
BufferedWriter out1= new BufferedWriter(fstream1); 
FileWriter fstream2=new FileWriter("outy.txt"); 
BufferedWriter out2= new BufferedWriter(fstream2); 
for(int i=0;i<320*240;i++) 
{ 

     out1.write(0+System.getProperty("line.separator"));// 
     // out1.write("\n"); 
     out2.write(0+System.getProperty("line.separator")); 
//out2.write("\n"); 

} 

:這裏在上面的代碼我把所有零 文件應該包含76800行(0),但我的文件只有69932行。 這是什麼問題,如果你可以建議一些其他方式來做到這一點。

回答

1

正如其他人指出的那樣,緩衝區中可能存在未刷新的數據。

重寫你的代碼會是這樣一種可接受的方式:

Writer out1 = new FileWriter("outx.txt"); 
try { 
    out1 = new BufferedWriter(out1); 
    Writer out2 = new FileWriter("outy.txt"); 
    try { 
    out2 = new BufferedWriter(out2); 
    for (int i = 0; i < 320 * 240; i++) { 
     out1.write(0 + System.getProperty("line.separator")); 
     out2.write(0 + System.getProperty("line.separator")); 
    } 
    } finally { 
    out2.close(); 
    } 
} finally { 
    out1.close(); 
} 

此代碼:

  • 將即使通過close
  • 總會釋放文件通過close處理數據刷新,發生錯誤(使用finally
  • 服從的合同類
  • 不會在空值或吞下例外
4

地址:

out1.flush(); 
out2.flush(); 

後for循環。

很可能您的程序在BufferedReader中的緩衝區被刷新之前退出,這是使用緩衝輸出的常見問題。

編輯:更正確的解決辦法是:

public static void main(String[] args) throws IOException { 
    final String outputString = "0" + System.getProperty("line.separator"); 
    BufferedWriter out1 = null; 
    BufferedWriter out2 = null; 
    try { 
     out1 = new BufferedWriter(new FileWriter("outx.txt")); 
     out2 = new BufferedWriter(new FileWriter("outy.txt")); 
     for(int i = 0; i < 320 * 240; i++) { 
      out1.write(outputString); 
      out2.write(outputString); 
     } 
     out1.flush(); // Not really needed as close will flush, but it is 
     out2.flush(); // useful for describing the intent of the code 
    } finally { 
     closeQuietly(out1); 
     closeQuietly(out2); 
    } 
} 

private static void closeQuietly(Closeable c) { 
    try { 
     if (c != null) { 
      c.close(); 
     } 
    } catch (Exception e) { 
     // No-op 
    } 
} 
+2

單獨沖洗是不夠的。您應該明確關閉流以釋放資源。 'close()'將已經隱含地刷新緩衝區。 – BalusC 2010-08-07 13:04:34

5

你記得關閉輸出流?你的例子沒有列出對close()的調用,也應該刷新流。 BufferedWriter的默認行爲是在關閉正在緩衝的流之前刷新(寫入)其剩餘內容。

您應該添加:

out1.close();
out2.close();

這是一個非常常見的情況,當文件的結尾被切斷時,您忘記關閉用於創建文件的寫入器,特別是當您使用BufferedOutputStream或BufferedWriter可能無法刷新其緩衝區時(寫入它到文件中),直到它被明確地刷新(或者更常見的是關閉)。

打開流後立即編寫close()調用,然後編寫所有代碼以處理調用之間的流是非常好的習慣。考慮例外情況考慮,該標準要求使用下列成語:

Writer myOutWriter = null; 
try { 
    myOutWriter = new BufferedWriter(new FileWriter("...")); 
    // Write to myOutWriter here 
} catch (IOException ioe) { 
    // Handle any exceptions here 
} finally { 
    try { 
     if (myOutWriter != null) { 
     myOutWriter.close(); 
     } 
    } catch (IOException ioe) { 
     // Not much you can do here 
    } 
} 

阿帕奇共享IO項目(http://commons.apache.org/io/)有一個很好的工具,叫做IOUtils.closeQuietly()的清理由包括嘗試捕捉finally塊,空檢查,並調用關閉到一個方法調用。使用該庫的示例如下所示:

Writer myOutWriter = null; 
try { 
    myOutWriter = new BufferedWriter(new FileWriter("...")); 
    // Write to myOutWriter here 
} catch (IOException ioe) { 
    // Handle any exceptions here 
} finally { 
    IOUtils.closeQuietly(myOutWriter); 
} 
+0

1)你可以重寫你的第一個代碼塊,以消除一個catch塊和一個毫無意義的空分配。 2)如果像這樣使用'closeQuietly',你的應用程序可能會向文件提交不完整的數據,並且不會處理該錯誤。這是因爲關閉可能會寫入數據。 – McDowell 2010-08-07 13:58:37

相關問題