我有这个代码创建一个文件并保存用户输入,但它会保持覆盖,我希望它保存用户提供的每个条目。我怎么能这样做?
File file = new File("info.txt");
BufferedWriter output = null;
try {
output = new BufferedWriter(new FileWriter(file));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
output.write("Users pick: " + myint+ "\t");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//file writer
答案 0 :(得分:3)
更改
output = new BufferedWriter(new FileWriter(file));
到
output = new BufferedWriter(new FileWriter(file,true));
以附加模式打开文件。