fr00ty_l00ps fr00ty_l00ps - 2 years ago 79
Java Question

Inserting New Lines when Writing to a Text File in 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??

Answer Source

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");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download