gpt4 book ai didi

带有重音字符的 Java 属性文件

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:44:51 25 4
gpt4 key购买 nike

我正在尝试从文本文件加载属性,但重音字符 (saül) 的编码不同于 UTF-8 如何避免?

我的属性文件有一个带有重音字符 (saül) 的属性。当我进行远程调试时,我发现 properties.load(bufferedReader); 将其作为 saül 因此当我写入另一个文件时,它被写入为 saül,我在应用程序的其他地方都有 UTF-8 编码。从文件中读取属性时,我不确定自己做错了什么。

try {
final String propertyFilePath = System.getProperty(JVM_ARGUMENT_NAME);
if (StringUtils.hasText(propertyFilePath)) {
setLocalOverride(true);
resource = getApplicationContext().getResource(propertyFilePath);
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(new FileInputStream(propertyFilePath), "UTF8"));
properties.load(bufferedReader);
externalFilePasswordConfigurer.afterPasswordPropertiesSet(properties);
LOGGER.info("ExternalFilePropertyConfigurer UTF-8 Reader");
}
setProperties(properties);
logProperties(properties);
} catch (Exception e) {
LOGGER.error("ExternalFilePropertyConfigurer setter failed to set properties: ", e);
throw new RuntimeException(e);
}

最佳答案

老问题,但据我所知,任何 .properties 文件都必须采用 ISO-8859-1 字符集,否则会有麻烦。

当属性文件中需要重音字符时,每个字符都必须替换为其 unicode 版本。在这种特殊情况下,“saül”必须更改为 “sa\u00FCl”,其中 \u00FC“ü”

另一种解决方案是将文件类型从 .properties 更改为 .xml

See java documentation here :

The load(Reader) / store(Writer, String) methods load and store properties from and to a character based stream in a simple line-oriented format specified below. The load(InputStream) / store(OutputStream, String) methods work the same way as the load(Reader)/store(Writer, String) pair, except the input/output stream is encoded in ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding can be written using Unicode escapes as defined in section 3.3 of The Java™ Language Specification; only a single 'u' character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.

关于带有重音字符的 Java 属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27363614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com