我尝试过使用 BufferWriter 格式以及 FileWriter 和 PrintWriter,每个都带有一个 boolean true 语句,但它们的行为就像我只是使用一个简单的新文件一样。每次程序运行结束时,我都会调用写入要附加的已保存数据的函数。最终发生的是它覆盖了最后保存的数据。我还有其他代码块也可以处理该文本文件,并且重新格式化它们也无济于事。
//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);
}
}