gpt4 book ai didi

java - 如何使用 Java 向文件添加内容

转载 作者:行者123 更新时间:2023-11-29 06:12:51 25 4
gpt4 key购买 nike

我有一个结果被输入到一个文件中。这个结果是在循环中完成的。因此,每次出现新结果时,都必须将其 append 到文件中,但它会被覆盖。我应该使用什么来将我的结果 append 到一个文件中?

最佳答案

尝试

BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter("filename", true));
out.write("aString");
}
catch (IOException e) {
// handle exception
}
finally{
if(out != null){
try{
out.close();
}
catch(IOException e){
// handle exception
}
}
}

根据API ,

Constructs a FileWriter object given a File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning.

关于java - 如何使用 Java 向文件添加内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6205078/

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