properties設定檔使用的是unicode碼。
不可以直接在裡面寫中文,JDK提供了
native2ascii這個方法。 1、只轉換特定字元
在控制台中可以輸入漢字斷行符號後,就可以看到轉移後的字元了。
Ctrl+C退出。
2、轉換properties檔案
native2ascii allMessages_zh_CN.input.properties allMessages_zh_CN.properties
將檔案allMessages_zh_CN.input.properties編碼後輸出為allMessages_zh_CN.properties。
為了方便properties檔案的管理,建議純中文的設定檔用input命名。
3、反向單一properties檔案
native2ascii -reverse allMessages_zh_CN.properties allMessages_zh_CN.txt
注意-reverse參數
4、批量反向所有的properties檔案
JDK內建的工具native2ascii可以將uncode編碼的檔案轉換為本地編碼的檔案,但是不能批量轉換檔。二、自己編寫轉換方法這樣,我們在程式裡處理起來會更靈活!
public static String str2unicode(final String gbString) {char[] utfBytes = gbString.toCharArray();String unicodeBytes = "";for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {String hexB = Integer.toHexString(utfBytes[byteIndex]);if (hexB.length() <= 2) {hexB = "00" + hexB;}unicodeBytes = unicodeBytes + "\\\\u" + hexB;}return unicodeBytes;}