Java Formatter不允许新行字符?

Cod*_*nny 1 java io newline file 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");
}
Run Code Online (Sandbox Code Playgroud)

唯一的问题是,输出没有换行符.

如果我用"\ _"替换"\n"也不起作用.

......我不知道为什么这不起作用.输出已创建并正确写入.(我看到创建的文件,所有内容都写在其中,除了换行符.)

jta*_*orn 9

您可以使用格式"%n"使用格式化程序输出特定于平台的换行符.


小智 6

无论运行什么平台,您都希望使用正确的换行符.您可以使用动态执行此操作

String newline = System.getProperty("line.separator");

所以你以后可以这样做

output.format(gR.toString() + newline);