tags:

views:

72

answers:

3

Is there some clean static method to just dump (append) a string to a file?

+1  A: 

Yes, see the documentation for FileWriter. Set the second argument to true to append to a file.

Swingley
That's not a clean static method, that's a block of code you need to write, cinluding stream handling, closing, etc.
skaffman
+2  A: 

No, the closest I am aware of is:

FileWriter writer = new FileWriter(fname, true);
writer.append(yourString);
writer.close();

It's not clean or static, but neither is it the most painful code. :)

James M.
uh... what is TextWriter?
skaffman
err, it's been a long day :)
James M.
Missing try...finally
ripper234
A: 

Use a PrintWriter like this

FileWriter fileWriter = new FileWriter(fileName, true); // true for append
PrintWriter printWriter = new PrintWriter(fileWriter);

printWriter.append(string);
printWriter.close();

PrintWriter Javadoc

Edit: Sorry I didn't check the right Javadoc for FileWriter. Nowadays it seems to have an append method.

So the PrintWriter isn't really necessary if all you want to do is append.

zockman
and then "clean static method" is where, exactly?
skaffman
Write your own if you really need one. Java does not have it.
zockman