Karl Jamoralin
Karl Jamoralin

Reputation: 1260

Newline in FileWriter

How do you produce a new line in FileWriter? It seems that "\n" does not work.

log = new FileWriter("c:\\" + s.getInetAddress().getHostAddress() + ".txt", true);    
log.append("\n" + Long.toString(fileTransferTime));
log.close();

The file output of the code above is just a long string of numbers without the new line.

Upvotes: 6

Views: 29028

Answers (4)

Martin
Martin

Reputation: 29

I'm using "\r\n" and it works great for me. Even when opening .txt document in notepad;)

Upvotes: 2

Colin Hebert
Colin Hebert

Reputation: 93197

You should either encapsulate your FileWriter into a PrintWriter if your goal is to have a formated content, println() will help you. Or use the system property line.separator to have a separator adapted to your Operating System.

System.getProperty("line.separator")

Resources :

Upvotes: 4

Tim Stone
Tim Stone

Reputation: 19189

I'll take a wild guess that you're opening the .txt file in Notepad, which won't show newlines with just \n.

Have you tried using your system-specific newline character combination?

log.append(System.getProperty("line.separator") + Long.toString(fileTransferTime));

Upvotes: 12

MAK
MAK

Reputation: 26586

Try changing \t to \n in the second line.

Upvotes: 1

Related Questions