Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Appending to an existing file  RSS feed

 
R Jarman
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!