• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Appending to an Existing File instead of Overwriting It

 
Ranch Hand
Posts: 246
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 246
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 246
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Kevin Simonson
Ranch Hand
Posts: 246
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic