我试图用以下代码从.properties文件中读取日语字符串值:
Properties properties = new Properties();
InputStream in = MyClass.class.getResourceAsStream(fileName);
properties.load(in);
问题显然在于上面的代码不识别文件的编码。它只读英文部分,用问号代替日语字符。顺便说一句,在编辑器中在Swing中显示日语文本或读取/写入UTF-8编码的.properties文件并不是一个问题。这两样东西都有用。
Properties
类编码不知情吗?是否有不违反通常在applet中找到的安全管理器设置的编码感知选项?
发布于 2011-12-07 14:15:19
load
期望ISO8859-1编码为在文档中注明.
通常,您需要使用native2ascii
来转换属性文件,使用读取器加载,或者在可以指定编码的地方使用XML。
发布于 2011-12-07 14:23:52
在我看来,您必须将日语字符转换为java Unicode转义字符串。
例如,这就是我对待越南人的方式。
Currency_Converter = Chuyen doi tien te
Enter_Amount = Nh\u1eadp v\u00e0o s\u1ed1 l\u01b0\u1ee3ng
Source_Currency = \u0110\u01a1n v\u1ecb g\u1ed1c
Target_Currency = \u0110\u01a1n v\u1ecb chuy\u1ec3n
Converted_Amount = K\u1ebft qu\u1ea3
Convert = Chuy\u1ec3n \u0111\u1ed5i
Alert_Mess = Vui l\u00f2ng nh\u1eadp m\u1ed9t s\u1ed1 h\u1ee3p l\u1ec7
Alert_Title = Thong bao
发布于 2017-01-12 21:29:26
可以从属性文件中读取任何语言,您应该做的就是从属性文件中按键获得值,然后创建一个新的字符串new String(keyValue.getBytes("ISO-8859-1"), "UTF-8")
,也就是说,它将为您创建一个UTF-8字符串。
public static String getLocalizedPropertyValue(String fileName, String key, Locale locale) throws UnsupportedEncodingException {
Properties props = getPropertiesByLocale(fileName, locale);
String keyValue = props.getProperty(key);
return keyValue != null ? new String(keyValue.getBytes("ISO-8859-1"), "UTF-8") : "";
}
https://stackoverflow.com/questions/8416680
复制相似问题