0
我有要求閱讀具有羅馬尼亞字符的字符串的屬性文件。無法對羅馬尼亞字符進行編碼
當我嘗試編碼時,我看到一些字符沒有正確編碼。
ResourceBundle bundle = ResourceBundle.getBundle("view.Country");
Enumeration keys = bundle.getKeys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
String value = bundle.getString(key);
try {
System.out.println(key + ": " + new String(value.getBytes("UTF-8"), "UTF-8"));
System.out.println("-----------");
System.out.println(key + ": " + new String(value.getBytes("Windows-1250"), "Windows-1250"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
輸入:
Country=România
AdministrativeAreaType=Țara
輸出:
AdministrativeAreaType: ?ara
-----------
AdministrativeAreaType: ?ara
Country: România
-----------
Country: România
正如你可以在輸出中看到,該國價值被正確編碼。但AdministrativeAreaType值編碼不正確。
請讓我知道如果代碼中有任何錯誤。
其中一個原因。輸入文件不是UTF-8或UNICODE。並在另一個說明中您是否嘗試過使用Locale的ResourceBUndle。像這個例子一樣http://www.avajava.com/tutorials/lessons/how-do-i-use-locales-and-resource-bundles-to-internationalize-my-application.html – Acewin
ResourceBundle文件始終處於ISO-8859-1編碼。您可能以不同的編碼保存了文件。 –
[如何使用ResourceBundle在資源屬性中使用UTF-8]的可能重複(http://stackoverflow.com/questions/4659929/how-to-use-utf-8-in-res- source-properties-with-resourcebundle) –