Most efficient way to write a string to a file

21,190

Solution 1

Use FileWriter will do you job.

BufferedWriter writer = new BufferedWriter( new FileWriter( yourfilename));
writer.write( yourstring);
// do stuff 
writer.close();

Solution 2

If you are planning to use the JDK alone, you might want to consider wrapping your BufferedWriter with a PrintWriter as in

PrintWriter printWriter=new PrintWriter(bufferedWriter);
printWriter.println(yourString);

The printWriter comes with a nice println method.

Or if you are free to use external libraries, try FileUtils writeStringToFile

Solution 3

Yup. Use java.io.BufferedWriter.

Share:
21,190
DavyGravy
Author by

DavyGravy

Updated on October 30, 2020

Comments

  • DavyGravy
    DavyGravy over 3 years

    Possible Duplicate:
    How do I save a String to a text file using Java?

    I want to add a reading from a machine to a file (Java). The reading will be in the form of a string that I have formatted. The file is just a text file. At the moment I am considering Filewriter/Buffered writer is this the correct one to use?