gpt4 book ai didi

Java文件写入时追加而不是覆盖?

转载 作者:行者123 更新时间:2023-12-01 22:36:13 25 4
gpt4 key购买 nike

我有这段代码可以创建一个文件并保存用户输入,但它不断覆盖,我希望它保存用户提供的每个条目。我怎样才能做到这一点?

    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

最佳答案

改变

output = new BufferedWriter(new FileWriter(file));

output = new BufferedWriter(new FileWriter(file,true));

用于以附加模式打开文件。

关于Java文件写入时追加而不是覆盖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26845647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com