• 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

Writing to a File without overwriting its contents

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does one write to an existing (non-empty) file without overwriting its contents? It's my understanding that FileWriter and FileOutputStream will overwrite an existing file. RandomAccessFile will work, but does RAF support character encoding?
Thanks!
Graff.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bob Graffagnino:
How does one write to an existing (non-empty) file without overwriting its contents? It's my understanding that FileWriter and FileOutputStream will overwrite an existing file. RandomAccessFile will work, but does RAF support character encoding?
Thanks!
Graff.


If you take a close look at the constructors available for FileOutputStream, you'll notice that there is one that takes a String for the filename and a boolean that says whether you'd like to append data or not. You can find that constructor here.
Just set that second parameter to true and you should be all set.
Corey
 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This may or may not be efficient but this is what I usually do. I read the file(assuming a text file) into a vector line by line and then what ever I want to add I just add to the end of the vector. Then use a writer to put it all back to the file.
It works well for a small number of small files. It may cause performance issues with a large number of files or files that are very long.
So, hope this helps.
 
Bob Graffagnino
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Geez, I can't believe I missed that! Would it be correct to assume that the FileOutputStream and File Writer constructors that don't take (String s, boolean b) as arguments overwrite an existing file? In other words, if one were to pass the constructor a File object or FileDescriptor object, an existing file would be overwritten?
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bob,
In this case you should write a small app, test the scenarios and see what happens. I believe this experimentation will solidify it for you.
 
eat bricks! HA! And here's another one! And a tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic