1

So, I've got the following code to write to a file:

Formatter output = ....... // Creating the formatter works, writes to appropriate file.

output.format("%d\n", records.length);
for(GradeRecord gR:records)
{
    output.format(gR.toString() + "\n");
}

Only problem is, the output doesn't have newline characters.

Doesn't work if I replace "\n" with "\r", either.

...I don't know why this doesn't work. Output is created and writes correctly. (I see the file created and everything is written in it, except for newline characters.)

CodeBunny
  • 1,991
  • 5
  • 22
  • 32

3 Answers3

12

you can use the format "%n" to output the platform specific newline using a formatter.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
6

You want to use the correct line break string regardless of what platform it's being run on. You can do this dynamically using

String newline = System.getProperty("line.separator");

So you can later do

output.format(gR.toString() + newline);

0

You can try using \\n instead of \n

thkala
  • 84,049
  • 23
  • 157
  • 201
Sripaul
  • 2,227
  • 9
  • 36
  • 60