Java FileWriter 中的换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3596766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Newline in FileWriter
提问by Karl Jamoralin
How do you produce a new line in FileWriter? It seems that "\n" does not work.
你如何在 FileWriter 中生成一个新行?似乎“\n”不起作用。
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.
上面代码的文件输出只是一长串没有换行的数字。
采纳答案by Tim Stone
I'll take a wild guess that you're opening the .txt
file in Notepad, which won't show newlines with just \n
.
我会胡乱猜测您是.txt
在记事本中打开文件,它不会仅显示\n
.
Have you tried using your system-specific newline character combination?
您是否尝试过使用系统特定的换行符组合?
log.append(System.getProperty("line.separator") + Long.toString(fileTransferTime));
回答by MAK
Try changing \t
to \n
in the second line.
尝试在第二行更改\t
为\n
。
回答by Colin Hebert
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.
如果您的目标是拥有格式化的内容,您应该将您的内容封装FileWriter
成一个PrintWriter
,这println()
将对您有所帮助。或者使用系统属性line.separator
来设置适合您的操作系统的分隔符。
System.getProperty("line.separator")
Resources :
资源 :
回答by Martin
I'm using "\r\n" and it works great for me. Even when opening .txt document in notepad;)
我正在使用 "\r\n" 并且它对我很有用。即使在记事本中打开 .txt 文档;)