【问题标题】:Avoiding to loose the line breaks (\n) while reading and writing files避免在读取和写入文件时松开换行符 (\n)
【发布时间】:2012-05-16 03:43:40
【问题描述】:

我阅读了几个属性文件,以将它们与模板文件进行比较以查找缺失的键。

FileInputStream compareFis = new FileInputStream(compareFile);
Properties compareProperties = new Properties();
compareProperties.load(compareFis);

注意:我以同样的方式阅读模板文件。

阅读后,我比较它们并将缺少的键及其值从模板文件写入一个集合。

CompareResult result = new CompareResult(Main.resultDir);
[...]
if (!compareProperties.containsKey(key)) {
    retVal = true;
    result.add(compareFile.getName(), key + "=" + entry.getValue());
}

最后我将丢失的键及其值写入一个新文件。

for (Entry<String, SortedSet<String>> entry : resultSet) {
    PrintWriter out = null;
    try {
        out = new java.io.PrintWriter(resultFile);
        SortedSet<String> values = entry.getValue();
        for (String string : values) {
            out.println(string);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        out.flush();
        out.close();
    }
}

如果我打开结果文件,我会看到模板文件值中的所有换行符“\n”都被替换为新行。示例:

test.key=Hello\nWorld!

变成

test.key=Hello
World!

虽然这基本上是正确的,但就我而言,我必须保留“\n”。

有谁知道如何避免这种情况?

【问题讨论】:

    标签: java file-io properties line-breaks


    【解决方案1】:

    由于您的输出似乎是一个属性文件,您应该使用Properties.store() 来生成输出文件。这不仅会处理换行符的编码,还会处理其他特殊字符(例如非 ISO8859-1 字符)。

    【讨论】:

      【解决方案2】:

      使用println 将以特定于平台的行终止符结束每一行。您可以改为明确编写您想要的行终止符:

      for (Entry<String, SortedSet<String>> entry : resultSet) {
          PrintWriter out = null;
          try {
              out = new java.io.PrintWriter(resultFile);
              SortedSet<String> values = entry.getValue();
              for (String string : values) {
                  out.print(string); // NOT out.println(string)
                  out.print("\n");
              }
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } finally {
              out.flush();
              out.close();
          }
      }
      

      【讨论】:

        【解决方案3】:

        使用Properties.store()为 JB Nizet 回答(我认为最好的)添加一个示例

            FileInputStream compareFis = new FileInputStream(compareFile);
            Properties compareProperties = new Properties();
            compareProperties.load(compareFis);
        
         ....
        
            StringBuilder value=new StringBuilder();
            for (Entry<String, SortedSet<String>> entry : resultSet) {
        
                    SortedSet<String> values = entry.getValue();
                    for (String string : values) {
                        value.append(string).append("\n");
                    }
            }
            compareProperties.setProperty("test.key",value);
            FileOutputStream fos = new FileOutputStream(compareFile);
            compareProperties.store(fos,null);
            fos.close();
        

        【讨论】:

          【解决方案4】:

          你需要这样的东西:

          "test.key=Hello\\nWorld!"
          

          其中"\\n" 实际上是\n

          【讨论】:

            【解决方案5】:

            在序列化之前转义 \n。如果你打算读取你输出的文件,你的阅读代码需要注意转义。

            【讨论】:

              【解决方案6】:

              您还可以查看 Apache Commons StringEscapeUtils.escapeJava( String )。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2023-03-23
                • 2020-02-24
                • 2015-03-25
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多