The most efficient way to write a string to a file

Possible duplicate:
How to save a string to a text file using Java?

I want to add a machine read to a file (Java). The reading will be in the form of a string, which I formatted. The file is a text file. I'm currently considering Filewriter / Buffered writer, is this the right option?

+4
source share
3 answers

Use FileWriter will do your job.

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

If you plan to use only the JDK, you may need to wrap your BufferedWriter with PrintWriter , as in

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

The printWriter script has a nice println method.

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

+6
source

Yeah. Use java.io.BufferedWriter .

+1
source

Source: https://habr.com/ru/post/1442933/


All Articles