Java將中文轉換為unicode碼(用於properties設定檔)

來源:互聯網
上載者:User

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;}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.