使用ResourceBundle加载properties文件
生活随笔
收集整理的這篇文章主要介紹了
使用ResourceBundle加载properties文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用ResourceBundle加載properties文件
@(JAVA)[java]
1、ResourceBundle介紹
說的簡單點,這個類的作用就是讀取資源屬性文件(properties),然后根據.properties文件的名稱信息(本地化信息),匹配當前系統的國別語言信息(也可以程序指定),然后獲取相應的properties文件的內容。
使用這個類,要注意的一點是,這個properties文件的名字是有規范的,一般的命名規范是: *自定義名_語言代碼_國別代碼.properties,如果是默認的,直接寫為:自定義名.properties
myres_en_US.properties myres_zh_CN.properties myres.properties當在中文操作系統下,如果myres_zh_CN.properties、myres.properties兩個文件都存在,則優先會使用myres_zh_CN.properties,當myres_zh_CN.properties不存在時候,會使用默認的myres.properties。
沒有提供語言和地區的資源文件是系統默認的資源文件。
資源文件都必須是ISO-8859-1編碼,因此,對于所有非西方語系的處理,都必須先將之轉換為Java Unicode Escape格式。轉換方法是通過JDK自帶的工具native2ascii.
2、示例一:英文環境
1、準備properties文件:person.properties
name=lujinhong age=30 gender=male2、java代碼
ResourceBundle rb = ResourceBundle.getBundle("person"); System.out.println(rb.getString("name") + "\t" + rb.getString("age") + "\t" +rb.getString("gender") );輸出如下:
lujinhong 30 male
3、示例二:中文環境
1、準備properties文件:person_zh_CN.properties
注意資源文件都必須是ISO-8859-1編碼。
name=\u9646\u9526\u6D2A age=30 gender=\u75372、java代碼
Locale locale = new Locale("zh","CN"); ResourceBundle rb2 = ResourceBundle.getBundle("person2",locale); System.out.println(rb2.getString("name") + "\t" + rb2.getString("age") + "\t" +rb2.getString("gender") );輸出如下:
陸錦洪 30 男
總結
以上是生活随笔為你收集整理的使用ResourceBundle加载properties文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: storm的消息格式分析
- 下一篇: MMDB ip地址库操作