YAML文件中的版权符号在构建的JAR文件中呈现不正确

时间:2014-05-27 06:24:34

标签: java yaml snakeyaml

我的Java程序使用Snake YAML来解析包含要显示给用户的文本的YAML文件。其中一些字符串包含版权符号(©)。当我在IDE(IntelliJ IDEA)中运行程序时,版权符号被正确呈现。 但是,一旦我构建了一个工件并运行生成的JAR文件,版权符号就会呈现为“©”(不带引号)。

如何更改我的程序以正确读取文件或更改YAML文件以便正确呈现版权符号?

以下是加载YAML的Java代码。

private void loadOptions () 
        throws IOException, SAXException, ParserConfigurationException
{
  Yaml yaml = new Yaml();
  String filePath = "./config.yml";
  Reader reader = null;

try {
    reader = new FileReader(filePath);
    options = (Map<String, Map>) yaml.load(reader);
  }
  catch (FileNotFoundException e) {
    String msg = "Either the YAML file could not be found or could not be read: " + e;
    System.err.println(msg);
  }

  if (reader != null) {
    reader.close();
  }
}

以下是有问题的YAML代码示例:

text:
  copyright:
    © 2007 Acme Publishing (info@example.org)

1 个答案:

答案 0 :(得分:1)

感谢@Amadan对我的问题的评论,我带领将我的Java代码更改为以下内容,这解决了问题:

private void loadOptions ()
    throws IOException, SAXException, ParserConfigurationException
{
  Yaml yaml = new Yaml();
  String filePath = "./config.yml";
  Reader reader = null;

  try {
    FileInputStream file = new FileInputStream(filePath);
    reader = new InputStreamReader(file, "UTF-8");
    options = (Map<String, Map>) yaml.load(reader);
  }
  catch (FileNotFoundException e) {
    String msg = "Either the YAML file could not be found or could not be read: " + e;
    System.err.println(msg);
  }

  if (reader != null) {
    reader.close();
  }
}