yogsma
yogsma

Reputation: 10584

Writing to new file

I am using FileOutputStream for writing some data to a file. Every time 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?

Upvotes: 2

Views: 2052

Answers (4)

Aaron Hathaway
Aaron Hathaway

Reputation: 4315

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

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

FileOutputStream fos = new  FileOutputStream("myfile");

would create a new file.

Upvotes: 3

BalusC
BalusC

Reputation: 1108722

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);

Upvotes: 7

Jon Skeet
Jon Skeet

Reputation: 1500535

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

new FileOutputStream(name, true)

? If so, just change true to false.

Upvotes: 4

Related Questions