Java Formatter 不允许换行符?
因此,我将以下代码写入文件:
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");
}
唯一的问题是,输出没有换行符。
如果我用“\r”替换“\n”也不起作用。
...我不知道为什么这不起作用。输出已创建并正确写入。 (我看到创建的文件,除了换行符之外,所有内容都写在其中。)
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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用格式“%n”通过格式化程序输出特定于平台的换行符。
you can use the format "%n" to output the platform specific newline using a formatter.
无论它在哪个平台上运行,您都希望使用正确的换行符字符串。 动态执行此操作,
您可以使用String newline = System.getProperty("line.separator");
因此您可以稍后执行
output.format(gR.toString() + newline);代码>
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);
您可以尝试使用
\\n
而不是\n
You can try using
\\n
instead of\n