【问题标题】:Java Formatter not allowing new line character?Java Formatter 不允许换行符?
【发布时间】:2011-03-31 02:04:03
【问题描述】:

所以,我有以下代码要写入文件:

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

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

如果我将“\n”替换为“\r”也不起作用。

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

【问题讨论】:

    标签: java file io newline formatter


    【解决方案1】:

    您可以使用格式“%n”通过格式化程序输出特定于平台的换行符。

    【讨论】:

      【解决方案2】:

      无论在哪个平台上运行,您都希望使用正确的换行符字符串。您可以使用

      动态执行此操作

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

      这样你以后可以做

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

      【讨论】:

        【解决方案3】:

        您可以尝试使用\\n 代替\n

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-02-02
          • 2017-04-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-12
          相关资源
          最近更新 更多