1

I've been working on sort of "logging" to text file using BufferedWriter and I came across a problem:

I run the following code.. fairly basic..

BufferedWriter out = new BufferedWriter(new FileWriter(path+fileName));
String str = "blabla";
out.write(str);
out.close();

and the next thing I know is that the entire file that had couple of lines of text has been cleared and only 'blabla' is there.

What class should I use to make it add a new line, with the text 'blabla', without having to get the entire file text to a string and adding it to 'str' before 'blabla'?

NucS
  • 619
  • 8
  • 21

3 Answers3

4

What class should I use to make it add a new line, with the text 'blabla', without having to get the entire file text to a string and adding it to 'str' before 'blabla'?

You're using the right classes (well, maybe - see below) - you just didn't check the construction options. You want the FileWriter(String, boolean) constructor overload, where the second parameter determines whether or not to append to the existing file.

However:

  • I'd recommend against FileWriter in general anyway, as you can't specify the encoding. Annoying as it is, it's better to use FileOutputStream and wrap it in an OutputStreamWriter with the right encoding.
  • Rather than using path + fileName to combine a directory and a filename, use File:

    new File(path, fileName);
    

    That lets the core libraries deal with different directory separators etc.

  • Make sure you close your output using a finally block (so that you clean up even if an exception is thrown), or a "try-with-resources" block if you're using Java 7.

So putting it all together, I'd use:

String encoding = "UTF-8"; // Or use a Charset
File file = new File(path, fileName);
BufferedWriter out = new BufferedWriter(
    new OutputStreamWriter(new FileOutputStream(file, true), encoding));
try {
   out.write(...);
} finally {
   out.close()'
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Snap, never noticed it had another constructor. I'll take those comments into my mind, thank you. – NucS Aug 04 '12 at 15:08
3

Try using FileWriter(filename, append) where append is true.

Jon Taylor
  • 7,865
  • 5
  • 30
  • 55
1
try {
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("outfilename", true)));
    out.println("the text");
    out.close();
} catch (IOException e) {
    //oh noes!
}

The above should work: Source Reference

Community
  • 1
  • 1
StoriKnow
  • 5,738
  • 6
  • 37
  • 46
  • I'd avoid `PrintWriter` if possible... do you *really* want exceptions to be silently swallowed? – Jon Skeet Aug 04 '12 at 15:02
  • Why avoid it? You can invoke 'checkErrors()' if needed. Although you're correct, it's not strictly necessary and if there's concern it can simply be removed. – StoriKnow Aug 04 '12 at 15:11
  • If you're going to call `checkErrors()`, why bother using `PrintWriter` at all? What is the *benefit* of using `PrintWriter`? All it buys you (that you should actually *want*) is a convenient way of writing a new-line. I'd rather not forsake "on-by-default" exceptions just for that. – Jon Skeet Aug 04 '12 at 15:13
  • That's where the **if needed** comes in from my last comment. It's a [convenience class](http://stackoverflow.com/questions/5765978/why-need-printwriter), if he's doing simple logging you don't always want or need exceptions to be thrown. If he is, however, worried about it, then removing it is the best choice, you're right. – StoriKnow Aug 04 '12 at 15:20