Insert new lines when writing to a text file in Java

I have a small mistake with learning FileWriter ... The ultimate goal is to write a program that will spawn a .bat file that will be executed by the batch code that launched .jar. The problem is that I do not know how to make sure that each FileWriter.write (); will print on a new line ... Any ideas?

+4
source share
3 answers

To create new lines, simply add a newline to the end of the line:

FileWriter writer = ... writer.write("The line\n"); 

In addition, the PrintWriter class provides methods that automatically add newline characters for you ( edit: it also automatically uses the correct newline line for your OS):

 PrintWriter writer = ... writer.println("The line"); 
+11
source

Use BufferedWriter and use writer.newLine () after each write operation that represents one line.

Or use PrintWriter and writer.println ().

+3
source

If you are using BufferedWriter , you can use the built-in method:

 BufferedWriter writer = Files.newBufferedWriter(output, charset); writer.newLine(); 
+1
source

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


All Articles