fr00ty_l00ps
fr00ty_l00ps

Reputation: 730

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??

Upvotes: 4

Views: 46624

Answers (3)

AalekhG
AalekhG

Reputation: 381

If you are using BufferedWriter then you can use an inbuilt method :

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

Upvotes: 1

Michael
Michael

Reputation: 35351

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");

Upvotes: 11

Polygnome
Polygnome

Reputation: 7820

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

Or, use a PrintWriter and writer.println().

Upvotes: 3

Related Questions