读写Java属性而不使用Unicode转义。

huangapple go评论77阅读模式
英文:

Read and Write Java Properties without unicode escapes

问题

这个问题可能已经被提问和回答了100次,但不幸的是,我没有找到适用于我的问题的任何合适的东西。

以下是情况:我遇到的问题是,当我读取属性进行更改然后再次写入时,所有特殊字符都会被转义为Unicode。

例如,":" 变成了 "\:" 或
Descripción 变成了 Descripci\u00F3n

是否有一种方法可以更改存储方法,以便特殊字符不会被转义?

非常感谢

这是我写入属性的代码:

  private static void writeUpdatedPropertiesFile(Properties newProperties, File sourceAndDestinationFile) {
    sourceAndDestinationFile.delete();
    try (FileOutputStream out = new FileOutputStream(sourceAndDestinationFile)) {
      newProperties.store(out, null);
    }
    catch (final IOException e) {
      e.printStackTrace();
    }
  }
英文:

this question has probably been asked and answered 100 times but unfortunately I have not found anything suitable for my issue.

The following situation: I have the problem that when I read properties to change them and then write them again, all special characters are unicode escaped.

for example ":" becomes "\:" or
Descripción becomes Descripci\u00F3n

Is there a way to change the store method so that the special characters are not escaped?

Thanks a lot

That's my code to write the properties:

  private static void writeUpdatedPropertiesFile(Properties newProperties, File sourceAndDestinationFile) {
    sourceAndDestinationFile.delete();
    try (FileOutputStream out = new FileOutputStream(sourceAndDestinationFile)) {
      newProperties.store(out, null);
    }
    catch (final IOException e) {
      e.printStackTrace();
    }
  }

答案1

得分: 1

你可以使用 store(Writer) 替代 store(OutputStream)。你可以使用任何字符集构造一个 OutputStreamWriter:

try (Writer out = new BufferedWriter(
    new OutputStreamWriter(
        new FileOutputStream(sourceAndDestinationFile),
        StandardCharsets.UTF_8))) {

    newProperties.store(out, null);

} catch (IOException e) {
    e.printStackTrace();
}

当然,你需要确保文件是一个 UTF-8 文件,并使用 load(Reader) 来读取它,而不是使用 InputStream:

try (Reader in = new BufferedReader(
    new InputStreamReader(
        new FileInputStream(sourceAndDestinationFile),
        StandardCharsets.UTF_8))) {

    properties.load(in);
} catch (IOException e) {
    // ...
}
英文:

You can use store(Writer) instead of store(OutputStream). You can construct an OutputStreamWriter with any charset you wish:

try (Writer out = new BufferedWriter(
    new OutputStreamWriter(
        new FileOutputStream(sourceAndDestinationFile),
        StandardCharsets.UTF_8))) {

    newProperties.store(out, null);

} catch (IOException e) {
    e.printStackTrace();
}

Of course, it is your responsibility to know that the file is a UTF-8 file, and to read it using load(Reader) instead of using an InputStream:

try (Reader in = new BufferedReader(
    new InputStreamReader(
        new FileInputStream(sourceAndDestinationFile),
        StandardCharsets.UTF_8))) {

    properties.load(in);
} catch (IOException e) {
    // ...
}

答案2

得分: 1

我用自定义的写入方法解决了这个问题:

private static void writeProperties(Properties properties, File destinationFile) {

    try (final BufferedWriter writer = new BufferedWriter(
        new OutputStreamWriter(new FileOutputStream(destinationFile), "Cp1252"))) {
      for (final Object o : properties.entrySet()) {
        final String keyValue = o.toString();
        writer.write(keyValue + "\r\n");
      }
    }
    catch (final IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }
英文:

I solved it with a custom writer method:

    private static void writeProperties(Properties properties, File destinationFile) {

    try (final BufferedWriter writer = new BufferedWriter(
        new OutputStreamWriter(new FileOutputStream(destinationFile), "Cp1252"))) {
      for (final Object o : properties.entrySet()) {
        final String keyValue = o.toString();
        writer.write(keyValue + "\r\n");
      }
    }
    catch (final IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }

huangapple
  • 本文由 发表于 2020年10月27日 21:25:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64555476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定