开发者

Newline in FileWriter

开发者 https://www.devze.com 2023-01-13 08:15 出处:网络
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);

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开发者_StackOverflow(fileTransferTime));
log.close();

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


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


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 :

  • JavaDoc - PrintWriter
  • JavaDoc - Properties available on System.getProperty


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消