aspose file tools*
The moose likes I/O and Streams and the fly likes Appending to an Existing File instead of Overwriting It Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Appending to an Existing File instead of Overwriting It" Watch "Appending to an Existing File instead of Overwriting It" New topic
Author

Appending to an Existing File instead of Overwriting It

Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 114
I do most of my file I/O with {Scanner} for input and {PrintWriter} for output. I've got lots of places in my code that looks like:

But when I call the constructor for {PrintWriter} up above, it overwrites whatever the original contents of the file designated by {dstntnName} were, doesn't it? Is there a way to call the constructor so that any future writes to it simply append to the original contents, instead of overwriting them?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2398
    
  50
Kevin Simonson wrote:But when I call the constructor for {PrintWriter} up above, it overwrites whatever the original contents of the file designated by {dstntnName} were, doesn't it? Is there a way to call the constructor so that any future writes to it simply append to the original contents, instead of overwriting them?

Yes, FileWriter has a constructor which you can pass a filename and a boolean value in as a second parameter. If you pass in 'true' it will append to an existing file.
Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 114
Tony Docherty wrote:
Kevin Simonson wrote:But when I call the constructor for {PrintWriter} up above, it overwrites whatever the original contents of the file designated by {dstntnName} were, doesn't it? Is there a way to call the constructor so that any future writes to it simply append to the original contents, instead of overwriting them?

Yes, FileWriter has a constructor which you can pass a filename and a boolean value in as a second parameter. If you pass in 'true' it will append to an existing file.

Thanks!
Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 114
Tony Docherty wrote:
Kevin Simonson wrote:But when I call the constructor for {PrintWriter} up above, it overwrites whatever the original contents of the file designated by {dstntnName} were, doesn't it? Is there a way to call the constructor so that any future writes to it simply append to the original contents, instead of overwriting them?

Yes, FileWriter has a constructor which you can pass a filename and a boolean value in as a second parameter. If you pass in 'true' it will append to an existing file.

So let's say I have code:

What happens if the file designated by {dstntnName} doesn't exist when this line gets executed? Does it throw an exception (since there's nothing to append to), or does it just open a file of zero length?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Kevin Simonson wrote:What happens if the file designated by {dstntnName} doesn't exist when this line gets executed? Does it throw an exception (since there's nothing to append to), or does it just open a file of zero length?

What happened when you tried it ?


Joanne
Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 114
Joanne Neal wrote:
Kevin Simonson wrote:What happens if the file designated by {dstntnName} doesn't exist when this line gets executed? Does it throw an exception (since there's nothing to append to), or does it just open a file of zero length?

What happened when you tried it ?

Okay; you got me; I should have tried it out myself to see what happens. I did just now try it out, and it turns out that if the file doesn't currently exist, the constructor creates an empty file, and successive {println()}s append to that empty file.
a gopinath
Greenhorn

Joined: Dec 22, 2012
Posts: 4
It creates an empty file and appends the data to the empty file.

Please find the below examples:

Example 1: it creates file file1.txt if doesnt exists in the drive.


Example 2 :



in example 2 it creates an blank file called file3.txt and appends "Data appended Successfully" data into the file if you use writer.flush();
other wise it creates and doesnt write any data into the file.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2398
    
  50
Welcome to the Ranch.

What happens when you create a FileWriter can depend on which operating system you are using and whether or not you have other write streams open for that file.
Assuming a Windows platform and no other open streams:
In your example 1 an empty file is created whether it currently exists or not. If a file it did exist all data will be lost.
In your example 2 not calling flush does not definitely mean data will not be written to the file it just means data will not be guaranteed to be written at that point in time. Data will be written if the FileWriters buffer is filled and also once the stream is closed (which isn't shown in your example but you should always do) all pending data is written to the file.
a gopinath
Greenhorn

Joined: Dec 22, 2012
Posts: 4
Tony Docherty,

you are right.But it depends up on the operating system and buffered size which is configued.
Ideal way is to use flush to ensure that all data is written into file not holding more time in buffer because of any server crashes.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2398
    
  50
a gopinath wrote:
you are right.But it depends up on the operating system and buffered size which is configued.
Ideal way is to use flush to ensure that all data is written into file not holding more time in buffer because of any server crashes.

Yes and No. You certainly don't want data to be hanging around in the FileWriter buffer so for instance if you have a stream that is open for a long period of time and you occasionally write to it then calling flush() after writing is a good idea but if you have lots of calls to write() for instance in a loop then putting a flush() inside the loop body can just negate the benefits of having a buffer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Appending to an Existing File instead of Overwriting It