Advertise with us

How can I save a String in a text file using Java?

How can I save a string in a text file?

Last updated:10/4/2013 9:30:40 AM

1 Answers

jacob rasel
jacob rasel

If you're simply outputting text, rather than any binary data, the following will work:

PrintWriter out = new PrintWriter("filename.txt");

Then, write your String to it, just like you would to any output stream:


You'll need exception handling, as ever. Be sure to call out.close() when you've finished writing.

Edit: further simplified per Jonik's comments.