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=ÇÇÇÇ
最佳答案
来自 1.5 javadoc“假定流使用 ISO 8859-1 字符编码” http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Properties.html#load(java.io.InputStream)
试试这个:
InputStream in = new ByteArrayInputStream(someStringWithUnicodeChars.getBytes("ISO-8859-1"));
Properties props = new Properties();
props.load(in);
关于java - JDK 1.5 属性加载 unicode 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24626836/