Java格式化器不允许新行字符?
题
所以,我有以下代码写入文件:
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”,则不起作用。
...我不知道为什么这不起作用。输出是创建并正确写入。(我看到创建的文件,除了换行符之外,所有内容都已写入其中。)
解决方案
you can use the format "%n" to output the platform specific newline using a formatter.
其他提示
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);
You can try using \\n
instead of \n
不隶属于 StackOverflow