Java的覆盖现有的输出文件 [英] Java overwriting an existing output file
问题描述
我的程序正在使用
FileOutputStream output = new FileOutputStream("output", true);
一个while循环创建如果尚未创建它的输出文件和一些数据追加到该文件使用while循环的每一次迭代
A while loop creates the output file if it is not yet created and appends some data to this file for every iteration of the while loop using
output.write(data).
这是很好,是我想要的。
This is fine and is what I want.
如果我再次运行该程序的文件只是的大小加倍,因为它附加的确切信息到文件的末尾。这不是我想要的。我想覆盖文件,如果我再次运行程序。
If I run the program again the file just doubles in size as it appends the exact information to the end of the file. This is not what I want. I would like to overwrite the file if I run the program again.
推荐答案
这文档建议你传递的参数是追加
参数。
This documentation suggests that the parameter you're passing is the append
parameter.
签名如下所示
FileOutputStream(File file, boolean append)
您应该设置该参数为假
,因为你不希望追加。
You should set that parameter to false
since you don't want to append.
FileOutputStream output = new FileOutputStream("output", false);
这篇关于Java的覆盖现有的输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!