本文介绍了File Writer会覆盖以前的Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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文件,但每次我完成写入过程都会覆盖它什么已经写在那里。如何更改我的代码,以便程序的这一部分不会覆盖已存在的代码?
I'm writing to a txt file, but every time I run through the writing process it overrides what is already written there. How can I change my code so this part of the program doesn't override what is already there?
推荐答案
将true传递为FileWriter打开追加模式的第二个参数。
Pass true as a second argument to FileWriter to turn on "append" mode.
FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
这篇关于File Writer会覆盖以前的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!