开发者

Writing to new file

I am using FileOutputStream for writing some data to a file. Every tim开发者_Go百科e I execute my program, it appends the data to existing file instead of creating new one. I want to create a new file every time I execute the program. How do I do that?


It doesn't append by default. It will only append when you use the constructor of FileOutputStream taking a boolean argument wherein you pass true. Just remove it.

In a nut, don't do

output = new FileOutputStream(file, true);

but rather do

output = new FileOutputStream(file);


By default, I believe it overwrites. Are you by any chance calling

new FileOutputStream(name, true)

? If so, just change true to false.


FileOutputStream fos = new  FileOutputStream("myfile");

would create a new file.


It looks like the FileOutputStream constructor accepts a second parameter. It is a boolean where false will cause file overwrite.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜