0
我有一個BufferedWriter
這是用來寫入文件的用戶的詳細信息。然而,我注意到,它並沒有以我想要的格式寫入文件,我似乎無法找到一種方法,它允許我在沒有寫入已存在或拷貝的文件的情況下在新行上寫入文本該文件中已有文本。有沒有什麼辦法可以編輯下面的代碼,讓它達到上述目的?如何阻止我的bufferedWriter重寫已存在於文件中的文本?
Details = "Name: " + name + " CardNo: " + CardNo + " Current Balance: " + balance + " overdraft? " + OverDraft + " OverDraftLimit: " + OverDraftLimit + " pin: " + PinToWrite;
try{
//Create writer to write to files.
File file = new File(DirToWriteFile);
FileOutputStream fos = new FileOutputStream(file, true);
Writer bw = new BufferedWriter(new OutputStreamWriter(fos, "UTF8"));
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader("VirtualATM.txt");
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
String currentData = "";
while((currentData=bufferedReader.readLine()) != null) {
line = currentData;
bw.write(currentData);
((BufferedWriter) bw).newLine();
}
bw.write(Details);
System.out.println("Account created!");
沒有與該程序沒有任何問題(所有variables
正常工作,並已申報的地方),我只貼這是有關問題的代碼片段。目前輸出到該文件運行幾次後看起來像this
您想要將新文本追加到文件的末尾,而不是替換文件的現有內容? – Kenster 2014-09-30 15:59:12
http://stackoverflow.com/questions/9199216/strings-written-to-file-using-bufferedwriter可能有幫助 – 2014-09-30 16:02:00