2014-11-20 45 views
1

我是一個慢慢學習Java的中等經驗的C++人。我正在寫一個程序,需要做到以下幾點:反覆寫入文件

  • 創建一個簡單的文本文件,默認目錄是罰款
  • 在程序運行時,定期寫數據的一行到文件中。根據多種因素,程序可能會向該文件寫入一次或一百萬次。沒有辦法知道哪個寫作是最後一個。

我一直在研究不同的方式來做到這一點,這是我想出的工作代碼。有兩個文件,「PeteProgram.java」和「PeteFileMgr.java」:

/* 
"PeteProgram.java" 
*/ 

import java.io.*; 
import java.lang.String; 

public class PeteProgram { 

    public static void main(String[] args) throws IOException { 

     String PeteFilename="MyRecordsFile.txt"; 
     Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(PeteFilename), "utf-8")); 
     PeteFileMgr MyPeteFileMgr = new PeteFileMgr(writer); 

     MyPeteFileMgr.AddThisString(writer, "Add this line #1\n"); 
     MyPeteFileMgr.AddThisString(writer, "Add this line #2\n"); 
     MyPeteFileMgr.AddThisString(writer, "Add this line #3\n"); 

    } 
} 


//===================================================================================================== 
//===================================================================================================== 


/* 
"PeteFileMgr.java" 
*/ 

import java.io.*; 

public class PeteFileMgr { 

    public PeteFileMgr(Writer writer) { 

     try { 
      writer.write("File created!"); 

      } catch (IOException ex) { 
      // report 
      } finally { 
      try {writer.close();} catch (Exception ex) {} 
      } 
     } 

    void AddThisString(Writer writer, String AddThis) { 

    try { 
      writer.append(AddThis); 

      } catch (IOException ex) { 
      // report 
      } finally { 
      try {writer.close();} catch (Exception ex) {} 
      } 
     } 
    } 

初始創建該文件的工作就好了。但是,待添加的行不會寫入文件中。由於該程序編譯並運行時沒有錯誤,因此我假定該程序嘗試寫入添加的行,失敗並引發異常。 (不幸的是,我正在使用原始編譯器/調試器,並且無法看到是否如此。)

有沒有人發現我的錯誤?

 Many thanks! 
      -P 

回答

3

這是因爲你沒有沖洗Writer。您應該不時致電flush。此外,您應該在您的應用程序末尾close您的Writer,而不是在寫入內容後。 close方法自動刷新作者的內容。

所以,這是你的代碼應該如何看起來像:

public class PeteProgram { 
    public static void main(String[] args) { 
     String peteFilename = "MyRecordsFile.txt"; 
     //here's when the physical file is created 
     Writer writer = null; 
     try { 
      writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(peteFilename), "utf-8")); 
     PeteFileMgr peteFileMgr = new PeteFileMgr(writer); 

     peteFileMgr.addThisString(writer, "Add this line #1\n"); 
     peteFileMgr.addThisString(writer, "Add this line #2\n"); 
     peteFileMgr.addThisString(writer, "Add this line #3\n"); 
     } catch (IOException e) { 
      //handle the exception 
      //basic handling 
      e.printStacktrace(); 
     } finally { 
      //this is a must! 
      try { writer.close(); } catch(IOException silent) { } 
     } 
    } 
} 

public class PeteFileMgr { 
    public PeteFileMgr(Writer writer) { 
     try { 
      //this method is not creating the physical file 
      writer.write("File created!"); 
     } catch (IOException ex) { 
      // report 
     } finally { 
      //remove this call to close 
      //try {writer.close();} catch (Exception ex) {} 
     } 
    } 

    public void addThisString(Writer writer, String addThis) { 
     try { 
      writer.append(addThis); 
     } catch (IOException ex) { 
      // report 
     } finally { 
      //remove this call to close 
      //try {writer.close();} catch (Exception ex) {} 
     } 
    } 
} 

或者,如果使用Java 7或優於使用try-with-resources

public class PeteProgram { 
    public static void main(String[] args) { 
     String peteFilename = "MyRecordsFile.txt"; 
     //here's when the physical file is created 
     try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(peteFilename), "utf-8"))) { 
     PeteFileMgr peteFileMgr = new PeteFileMgr(writer); 

     peteFileMgr.addThisString(writer, "Add this line #1\n"); 
     peteFileMgr.addThisString(writer, "Add this line #2\n"); 
     peteFileMgr.addThisString(writer, "Add this line #3\n"); 
     } catch (IOException e) { 
      //handle the exception 
      //basic handling 
      e.printStacktrace(); 
     } 
    } 
} 
+0

到'writer.close()的調用應該'仍然在'main'的'finally'塊中。 – 2014-11-20 20:01:54

+0

優秀!這工作出色......!非常感謝 :) – Pete 2014-11-21 19:58:44