Java語(yǔ)言對(duì)properties資源文件的處理
開(kāi)始之前,我們先解釋一下什么是properties類型的資源文件。
在Java語(yǔ)言中,使用一種以.properties為擴(kuò)展名的文本文件作為資源文件,該類型的文件的內(nèi)容格式為類似:
#注釋語(yǔ)句
some_key=some_value
形式。以#開(kāi)頭的行作為注釋行,ResourceBundle類處理時(shí)會(huì)加以忽略;其余的行可以以 key名=value值 的形式加以記述。
Java的ResourceBundle類可以對(duì)這種形式的文件加以處理。
ResourceBundle類的使用方法也非常簡(jiǎn)單。我們使用一個(gè)例子來(lái)說(shuō)明。
我們假設(shè)有下面2個(gè)properties文件:
- TestProperties.properties
- view plainprint?
- #key=value
- userIdLabel=User Id:
- userNameLabel=User Name:
- #key=value
- userIdLabel=User Id:
- userNameLabel=User Name:
- TestProperties_zh_CN.properties
- view plainprint?
- #key=value
- userIdLabel=用戶ID:
- userNameLabel=用戶名:
- #key=value
- userIdLabel=用戶ID:
- userNameLabel=用戶名:
大家可能注意到TestProperties_zh_CN.properties文件名中有一個(gè)_zh_CN名稱,該名稱其實(shí)是用于資源文件的本地化處理。什么是本地化呢?我們簡(jiǎn)單說(shuō)明一下:我們?cè)谶M(jìn)行系統(tǒng)開(kāi)發(fā)時(shí),很多時(shí)候需要為不同地區(qū)的用戶準(zhǔn)備不同的界面,比如,如果一個(gè)系統(tǒng)同時(shí)面向 英語(yǔ)圈的用戶以及面向中國(guó)的用戶,我們就必須為系統(tǒng)準(zhǔn)備2套界面(包括消息),一套為英語(yǔ)界面,一套為中文界面。當(dāng)然,除了界面不同之外,系統(tǒng)的處理過(guò)程完全一樣。當(dāng)然我們不可能為它們分別開(kāi)發(fā)2套不同的系統(tǒng),怎么辦呢?這就需要用到資源的本地化處理。也就是說(shuō),根據(jù)用戶所處的地區(qū)或語(yǔ)言的不同,分別準(zhǔn)備不同的資源文件,這樣就可以為不同的用戶準(zhǔn)備不同的界面但使用的卻是同一套系統(tǒng)邏輯。
我們上面的2個(gè)文件就是2套不同的資源。
我們是使用ResourceBundle類處理不同資源的代碼:
- TestProperties.java
- view plainprint?
- package com.test.properties;
- import java.util.Enumeration;
- import java.util.Locale;
- import java.util.ResourceBundle;
- public class TestProperties {
- public static void main(String []args) {
- String resourceFile = "com.test.properties.TestProperties";
- //創(chuàng)建一個(gè)默認(rèn)的ResourceBundle對(duì)象
- //ResourceBundle會(huì)查找包c(diǎn)om.test.properties下的TestProperties.properties的文件
- //com.test.properties是資源的包名,它跟普通java類的命名規(guī)則完全一樣:
- //- 區(qū)分大小寫(xiě)
- //- 擴(kuò)展名 .properties 省略。就像對(duì)于類可以省略掉 .class擴(kuò)展名一樣
- //- 資源文件必須位于指定包的路徑之下(位于所指定的classpath中)
- //另外,對(duì)于非西歐字符(比如中日韓文等),需要使用native2ascii命令或類似工具將其轉(zhuǎn)換成ascii碼文件格式,否則會(huì)顯示亂碼。
- System.out.println("---Default Locale---");
- ResourceBundle resource = ResourceBundle.getBundle(resourceFile);
- testResourceBundle(resource);
- System.out.println("---Locale.SIMPLIFIED_CHINESE---");
- //創(chuàng)建一個(gè)指定Locale(本地化)的ResourceBundle對(duì)象,這里指定為L(zhǎng)ocale.SIMPLIFIED_CHINESE
- //所以ResourceBundle會(huì)查找com.test.properties.TestProperties_zh_CN.properties的文件
- //
- //中文相關(guān)的Locale有:
- //Locale.SIMPLIFIED_CHINESE : zh_CN
- resource = ResourceBundle.getBundle(resourceFile, Locale.SIMPLIFIED_CHINESE);
- //Locale.CHINA : zh_CN
- //Locale.CHINESE: zh
- testResourceBundle(resource);
- //顯示
- //
- }
- private static void testResourceBundle(ResourceBundle resource) {
- //取得指定關(guān)鍵字的value值
- String userIdLabel = resource.getString("userIdLabel");
- System.out.println(userIdLabel);
- //取得所有key值
- Enumeration enu = resource.getKeys();
- System.out.println("keys:");
- while(enu.hasMoreElements()) {
- System.out.println(enu.nextElement());
- }
- }
- }
- package com.test.properties;
- import java.util.Enumeration;
- import java.util.Locale;
- import java.util.ResourceBundle;
- public class TestProperties {
- public static void main(String []args) {
- String resourceFile = "com.test.properties.TestProperties";
- //創(chuàng)建一個(gè)默認(rèn)的ResourceBundle對(duì)象
- //ResourceBundle會(huì)查找包c(diǎn)om.test.properties下的TestProperties.properties的文件
- //com.test.properties是資源的包名,它跟普通java類的命名規(guī)則完全一樣:
- //- 區(qū)分大小寫(xiě)
- //- 擴(kuò)展名 .properties 省略。就像對(duì)于類可以省略掉 .class擴(kuò)展名一樣
- //- 資源文件必須位于指定包的路徑之下(位于所指定的classpath中)
- //另外,對(duì)于非西歐字符(比如中日韓文等),需要使用native2ascii命令或類似工具將其轉(zhuǎn)換成ascii碼文件格式,否則會(huì)顯示亂碼。
- System.out.println("---Default Locale---");
- ResourceBundle resource = ResourceBundle.getBundle(resourceFile);
- testResourceBundle(resource);
- System.out.println("---Locale.SIMPLIFIED_CHINESE---");
- //創(chuàng)建一個(gè)指定Locale(本地化)的ResourceBundle對(duì)象,這里指定為L(zhǎng)ocale.SIMPLIFIED_CHINESE
- //所以ResourceBundle會(huì)查找com.test.properties.TestProperties_zh_CN.properties的文件
- //
- //中文相關(guān)的Locale有:
- //Locale.SIMPLIFIED_CHINESE : zh_CN
- resource = ResourceBundle.getBundle(resourceFile, Locale.SIMPLIFIED_CHINESE);
- //Locale.CHINA : zh_CN
- //Locale.CHINESE: zh
- testResourceBundle(resource);
- //顯示
- //
- }
- private static void testResourceBundle(ResourceBundle resource) {
- //取得指定關(guān)鍵字的value值
- String userIdLabel = resource.getString("userIdLabel");
- System.out.println(userIdLabel);
- //取得所有key值
- Enumeration enu = resource.getKeys();
- System.out.println("keys:");
- while(enu.hasMoreElements()) {
- System.out.println(enu.nextElement());
- }
- }
- }
解說(shuō):
1,為了便于理解,我們把解說(shuō)放在Java源代碼中了,這里不再詳述了。
2,對(duì)于中文資源文件TestProperties_zh_CN.properties,需要使用native2ascii 命令將其轉(zhuǎn)換為ascii碼。例如:
native2ascii -encoding UTF-8 c:\TestProperties_zh_CN.properties c:\java\com\test\properties\TestProperties_zh_CN.properties
至于native2ascii的詳細(xì)用法這里不做詳述了。
3,將上面3個(gè)文件都保存在 c:\java\com\test\properties\ 目錄下。其中TestProperties_zh_CN.properties為經(jīng)過(guò)native2ascii轉(zhuǎn)換后的文件。
4,編譯執(zhí)行,將會(huì)在屏幕上顯示:
c:\java\javac com.test.properties.TestProperties.java
c:\java\java com.test.properties.TestProperties
---Default Locale---
User Id:
keys:
userNameLabel
userIdLabel
---Locale.SIMPLIFIED_CHINESE---
用戶ID:
keys:
userNameLabel
userIdLabel
【編輯推薦】