“RATNAGIRI GAS \u0026 POWER”是属性文件中的数据。当读取到应用程序时,它会转换为“RATNAGIRI GAS & POWER”。这里发生的是字符类型转换。如何停止这种自动转换?
这就是我加载属性文件的方式。请分享您对发生这种情况的原因以及如何读取属性文件中的字符串的想法。
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream(properties_name);
property.load(in);
最佳答案
我认为没有一种干净的方法可以做到这一点。
您可以做的是转义属性文件中的 unicode 表示形式,或者将所选字符转换回其 unicode 表示形式。
下面是第一个“解决方案”的小示例。对这个答案不是很自豪。
代码
Properties p = new Properties();
InputStream s = Main.class.getClassLoader().getResourceAsStream("test/test.properties");
try {
p.load(s);
System.out.println(p.get("key"));
System.out.println(p.get("rawKey"));
}
catch (IOException ioe) {
ioe.printStackTrace();
}
测试包中的test.properties文件
key=\u0026
rawKey=\\u0026
输出
&
\u0026
您可能想检查this对于“转换回unicode”“解决方案”。
关于java - 从属性读取数据而不解析 Unicode 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19867424/