【问题标题】:PrintWriter#println method not working as expectedPrintWriter#println 方法未按预期工作
【发布时间】:2012-10-09 08:07:08
【问题描述】:

这段代码:

PrintWriter output = new PrintWriter(new FileWriter(outputFile, false));
output.println("something\n");
output.println("something else\n");

输出:

something
something else

代替:

something

something else

我尝试使用“\r\n”而不是“\n”,但它并没有像我想要的那样工作。我该如何解决这个问题?

附:我正在使用 Windows 7

【问题讨论】:

  • 使用 -- output.print("something\n\n");
  • 你能发布输出文件的十六进制转储吗?
  • 什么 output.println("something"); - 没有任何 \n - 打印?
  • 您是否正在使用记事本检查文件内容?它不会将单个 \n 显示为换行符,但它存在,因为您可以在使用不同的文本编辑器甚至 hexeditor 显示它时验证它。
  • 查看this 并使用高级文本编辑器检查输出,以便查看实际输出的字符。

标签: java printwriter


【解决方案1】:

您可以连接系统的换行符来分隔您的行:

    String newLine = System.getProperty("line.separator");
    output.println("something" + newLine);
    output.println("something else" + newLine);

【讨论】:

  • 只有在程序运行时在同一系统中查看文件时,这也保证有效。但它比使用\n\r\n 左右更干净。 +1
【解决方案2】:

您的代码就像一个魅力,只需使用适当的程序员编辑器检查文件。 (或者按照我之前的建议,查看文件的十六进制转储)

【讨论】:

    【解决方案3】:

    这个

    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class Main {
        public static void main(String[] args) {
            PrintWriter output;
            try {
                output = new PrintWriter(new FileWriter("asdf.txt", false));
                output.println("something\n");
                output.println("something else\n");
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    对我来说很好用,我得到了一个像这样的 asdf.txt

    某事

    其他的

    我用的是jre1.7,你用的是什么?

    【讨论】:

    • 问题不是java。如上所述,我下载了一个高级编辑器(notepad++),一切都很好。感谢您回复我的帖子!
    • Oõ 您使用的哪个编辑器无法正确显示\n
    【解决方案4】:

    这工作得很好。您必须使用记事本进行输出。尝试使用其他文本编辑器,例如 notepad++。你会得到你想要的输出。

    【讨论】:

      【解决方案5】:

      试试这个:

      package com.stackoverflow.works;
      
      import java.io.FileWriter;
      import java.io.PrintWriter;
      
      /*
       * @author: sarath_sivan
       */
      public class PrintWriterExample {
      
          private static final String NEW_LINE = System.getProperty("line.separator");
      
          public static void main(String[] args) {
              String outputFile = "C:/Users/sarath_sivan/Desktop/out.txt";
              PrintWriter output = null;
              try {
                  output = new PrintWriter(new FileWriter(outputFile, false));
                  output.println("something" + NEW_LINE);
                  output.println("something else" + NEW_LINE);
                  output.flush();
              } catch(Exception exception) {
                  exception.printStackTrace();
              } finally {
                  if (output != null) {
                      output.close();
                  }
              }
          }
      }
      

      输出:

      【讨论】:

        猜你喜欢
        • 2019-04-22
        • 2015-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-07
        • 2020-10-04
        • 2018-12-22
        • 1970-01-01
        相关资源
        最近更新 更多