Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PrintWriter#println method not working as expected

This code:

PrintWriter output = new PrintWriter(new FileWriter(outputFile, false));
output.println("something\n");
output.println("something else\n");

Outputs:

something
something else

Instead of:

something

something else

I tried using "\r\n" instead of just "\n" but it just doesn't work like how I want it to. How do I fix this?

P.S. I'm using windows 7

like image 651
Haque1 Avatar asked Dec 21 '22 15:12

Haque1


2 Answers

You can concatenate system's newline to separate your lines:

    String newLine = System.getProperty("line.separator");
    output.println("something" + newLine);
    output.println("something else" + newLine);
like image 99
Juvanis Avatar answered Dec 24 '22 02:12

Juvanis


Your code works like a charm, just check the file with a proper programmers editor. (or as I suggested before, take a look at an hex dump of the file)

like image 43
BigMike Avatar answered Dec 24 '22 02:12

BigMike