【发布时间】:2014-07-08 18:11:20
【问题描述】:
JDK 1.5 属性 load 方法仅获取 InputStream 而 JDK 1.6+ load 方法也获取 Reader。当使用 load(reader) 将带有 Unicode 字符的字符串加载到 JDK 1.6+ 上的属性对象时,没有问题。但是在 JDK 1.5 上只有 load(InputStream) 方法;加载到属性时,未正确加载 unicode 字符。
Properties props = new Properties();
ByteArrayInputStream bis = null;
Reader reader = null;
try {
bis = new ByteArrayInputStream(someStringWithUnicodeChars.getBytes("UTF-8"));
reader = new InputStreamReader(bis, "UTF-8");
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
props.load(reader); // This reads unicode characters correctly on JDK 1.6+
// There is no props.load(reader) method on JDK 1.5, so below method is used
props.load(bis);
// but Unicode characters are not loaded correctly.
如何将以下带有 unicode 字符的示例字符串加载到属性对象。
key1=test İ Ş Ğ
key2=ÇÇÇÇ
【问题讨论】:
-
一般来说,您需要在属性文件中使用 unicode 转义符,以便使用
InputStream(即 \u00FF 类型的替换)正确加载它们。