• 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

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a file that I am using as a log file. I want to be able to write to it from within different parts of my program. I don't want to clear the file everytime I open it. I created a method that takes two strings as arguments, one is the filename and the other is the string to write to the file. I instantiate a PrintWriter for the filename argument and writes the other string to my file. Since I instantiate a new PrintWriter everytime I call the method, it creates or overwrites the file, right? I use this method also to write other information to another file. Is there a way to tell the PrintWriter not to overwrite the existing file? If not, any suggestions on how I can write to a log file from several different classes?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're probably using a FileWriter or FileOutputStream somewhere, is that right? Check the API for these classes and look at the constructors - some of the constructors let you specify whether you will overwrite or append.
 
R Jarman
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I'm using a PrintWriter which is supposed to encapsulate instantiating all of the other writer classes. It doesn't have an append option in it constructor.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Since I instantiate a new PrintWriter everytime I call the method, it creates or overwrites the file, right?

Right. So just create the PrintWriter once and pass it around, rather than passing the file name around and creating the PrintWriter many times.

Or better still, write a method that writes log information to that PrintWriter and have any method that wants to log call that method. (This is called "encapsulation" if you didn't know that.) Then you can change your logging from PrintWriter to some other technique without having to change the signature of dozens of other methods.
[ November 09, 2006: Message edited by: Paul Clapham ]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh I see, you're using new features from JDK 1.5. Unfortunately, PrintWriter does not encapsulate all the possible features you might need for writing to files - specifically, there's no way to tell it to append to an existing file unless you use a FileWriter or FileOutputStream - you need one of the constructors that takes a boolean, allowing you to specify appending rather than overwriting. You can learn more about how to combine different Readers and Writers here if you haven't seen it before.
 
reply
    Bookmark Topic Watch Topic
  • New Topic