2013-02-09 23 views
0

越來越ovewritten我使用BufferWriter格式以及嘗試的FileWriter和PrintWriter的每一個true語句,但他們都表現得一樣的,如果我只是用一個簡單的新文件。每次我運行程序結束時,都會調用寫入保存的數據的函數。最終發生的是它覆蓋最後保存的數據。我也有其他代碼塊來處理該文本文件,並重新格式化它們也沒有做任何事情。輸出flles一直在Java中

//saves user information to "Users.txt" which will be called in the finally block after choice switch 
public void writeUsers() 
{ 
    try{ 

     File userFile = new File("Users.txt"); 
     PrintWriter output = new PrintWriter(userFile); 
     for(User i: userList) {  
     output.append("Name:");  
     output.println(i.getrealName()); 
     output.append("UID:"); 
     output.println(i.getidName()); 
     output.append("Password:"); 
     output.println(i.getpassword()); 
     output.println(" "); 
     } 
     output.close(); 
     System.out.print("Information has been saved to Users.txt\n"); 


    } 
    catch(FileNotFoundException fnf) { 
     System.err.println("FileNotFoundException: File Users.txt does not exist " + fnf); 
    } 
    catch(IOException eyeoh) { 
     System.err.println("IOException: Error writing to Users.txt " + eyeoh); 
    } 
} 

回答

1

你必須追加模式創建PrintWriter。否則,當它第一次打開文件時,它將清除它。您可以使用追加方式打開它:

new PrintWriter(new FileWriter(userFile,true)) // the `true` argument opens it in append mode 
2

構造PrintWriter(File)截斷默認輸出文件。調用PrintWriter的方法append()這一事實並不意味着它改變了正在打開的文件的模式。對於append行爲是described爲:

形式out.append(CSQ)的這個方法調用完全相同的行爲方式調用

out.write(csq.toString()) 

在這裏,你可以使用由德福附加

PrintWriter output = 
    new PrintWriter(new FileOutputStream(userFile, true /* append = true */)); 
1

PrintWriter的需要FileOutputStreamPrintWriter構造alt截斷所有現有數據。爲了追加,你可以像其他答案所建議的那樣,向構造函數添加一個「true」參數,它表示「append = true」,但是,使用java.nio.file可以更優雅地完成此操作。 Files以及java.nio.file。 StandardOpenOption,您可以在其中指定StandardOpenOption.APPEND而不是StandardOpenOption.TRUNCATE_EXISTING 你也可以指定的東西,如StandardOpenOption.CREATE如果不存在,它創建的文件。

此外,記得無論是放置在一個finally塊你output.close()聲明,或使用try-與資源。否則,如果程序流被中斷(即拋出異常),output將保持未關閉狀態。我個人使用try-with-resources,因爲它沒有什麼麻煩:只需聲明所有資源,並且它們會自動關閉,無論程序流是否中斷。

此外,作爲一般提示,打印或傳遞catch塊中的實際Exception對象,而不僅僅是「自定義字符串」,以便不丟失拋出的原始內容Exception。然後,您可以將其與任何您想要打印的字符串連接起來。

try(BufferedWriter bufWriter = 
     Files.newBufferedWriter(Paths.get("Users.txt"), 
      Charset.forName("UTF8"), 
      StandardOpenOption.WRITE, 
      StandardOpenOption.APPEND, //Makes this BufferedWriter append to the file, not truncate 
      StandardOpenOption.CREATE); 
    PrintWriter output = new PrintWriter(bufWriter, true);) 
{ 

    output.println("Text to be appended."); 

}catch(FileNotFoundException e){ 
    System.err.println(e + "Custom string"); 
}catch(IOException e){ 
    System.err.println(e + "Something eyeoh occurred..."); 
} 

此使用try-與資源語句來聲明和BufferedWriter使用java.nio.file.Files,它接受StandardOpenOption參數創建和自動沖洗PrintWriter從所得到的BufferedWriter(由「真」在構造函數中表示) 。然後可以調用PrintWriterprintln()方法寫入文件。

該代碼中使用的參數StandardOpenOption:打開文件進行寫入,僅附加到文件,並在文件不存在時創建該文件。

Paths.get("path here")可以與new File("path here").toPath()被替換,如果專門與File對象(即,如果使用的是JFileChooser.getSelectedFile()) 而且Charset.forName("charset name")可被修改以適應所需Charset工作。