如何使 BufferedWriter 不覆盖文本文件
How to make BufferedWriter not overwrite a text file
我目前正在制作一个用户可以添加多个帐户的应用程序。登录信息存储在文本文件中。我需要一种方法让用户添加新帐户,而 BufferedWriter 不会覆盖其他帐户的信息。有什么建议么?
package mainFiles.fileManagers;
import java.io.*;
public class NewTXT {
public static void nFile( String a , String b ) throws IOException {
File f = new File("mainFiles\storage\accountinfo.txt");
FileWriter writer = new FileWriter("mainFiles\storage\accountinfo.txt");
BufferedWriter bWriter = new BufferedWriter(writer);
f.createNewFile();
f.canWrite();
bWriter.newLine();
bWriter.write(a + "?" + b );
bWriter.newLine();
bWriter.close();
}
}
BufferedWriter
没有问题。您必须在追加模式中构建 FileWriter
:
FileWriter writer = new FileWriter("mainFiles\storage\accountinfo.txt", true);
查看 FileWriter 文档:
https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter(java.lang.String,%20boolean)
我目前正在制作一个用户可以添加多个帐户的应用程序。登录信息存储在文本文件中。我需要一种方法让用户添加新帐户,而 BufferedWriter 不会覆盖其他帐户的信息。有什么建议么?
package mainFiles.fileManagers;
import java.io.*;
public class NewTXT {
public static void nFile( String a , String b ) throws IOException {
File f = new File("mainFiles\storage\accountinfo.txt");
FileWriter writer = new FileWriter("mainFiles\storage\accountinfo.txt");
BufferedWriter bWriter = new BufferedWriter(writer);
f.createNewFile();
f.canWrite();
bWriter.newLine();
bWriter.write(a + "?" + b );
bWriter.newLine();
bWriter.close();
}
}
BufferedWriter
没有问题。您必须在追加模式中构建 FileWriter
:
FileWriter writer = new FileWriter("mainFiles\storage\accountinfo.txt", true);
查看 FileWriter 文档: https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter(java.lang.String,%20boolean)