Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting New Lines when Writing to a Text File in Java

Tags:

java

I have a slight delema with learning FileWriter... The ultimate goal is writing a program that will "spawn" a .bat file that will be executed by the batch code that launched the .jar. The problem is, I have no clue how to make sure that every FileWriter.write(); will print on a new line... Any ideas??

like image 789
fr00ty_l00ps Avatar asked Dec 06 '22 14:12

fr00ty_l00ps


1 Answers

To create new lines, simply append a newline character to the end of the string:

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

Also, the PrintWriter class provides methods which automatically append newline characters for you (edit: it will also automatically use the correct newline string for your OS):

PrintWriter writer = ...
writer.println("The line");
like image 105
Michael Avatar answered Dec 09 '22 03:12

Michael