try {
File file = new File(filePath+"usedcommands.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(input+"\n");
bw.close();
} catch(Exception e) { System.out.println("can't write to usedcommands.txt..."); }
我正在写入txt文件,但是每次我执行写入过程时,它都会覆盖那里已经写入的内容.如何更改代码,以使程序的这一部分不会覆盖已经存在的内容?
解决方法:
将true作为第二个参数传递给FileWriter以打开“附加”模式.
FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
标签:bufferedwriter,java,text
来源: https://codeday.me/bug/20191013/1908644.html