Java語言對properties資源文件的處理
開始之前,我們先解釋一下什么是properties類型的資源文件。
在Java語言中,使用一種以.properties為擴展名的文本文件作為資源文件,該類型的文件的內容格式為類似:
#注釋語句
some_key=some_value
形式。以#開頭的行作為注釋行,ResourceBundle類處理時會加以忽略;其余的行可以以 key名=value值 的形式加以記述。
Java的ResourceBundle類可以對這種形式的文件加以處理。
ResourceBundle類的使用方法也非常簡單。我們使用一個例子來說明。
我們假設有下面2個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文件名中有一個_zh_CN名稱,該名稱其實是用于資源文件的本地化處理。什么是本地化呢?我們簡單說明一下:我們在進行系統開發時,很多時候需要為不同地區的用戶準備不同的界面,比如,如果一個系統同時面向 英語圈的用戶以及面向中國的用戶,我們就必須為系統準備2套界面(包括消息),一套為英語界面,一套為中文界面。當然,除了界面不同之外,系統的處理過程完全一樣。當然我們不可能為它們分別開發2套不同的系統,怎么辦呢?這就需要用到資源的本地化處理。也就是說,根據用戶所處的地區或語言的不同,分別準備不同的資源文件,這樣就可以為不同的用戶準備不同的界面但使用的卻是同一套系統邏輯。
我們上面的2個文件就是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";
- //創建一個默認的ResourceBundle對象
- //ResourceBundle會查找包com.test.properties下的TestProperties.properties的文件
- //com.test.properties是資源的包名,它跟普通java類的命名規則完全一樣:
- //- 區分大小寫
- //- 擴展名 .properties 省略。就像對于類可以省略掉 .class擴展名一樣
- //- 資源文件必須位于指定包的路徑之下(位于所指定的classpath中)
- //另外,對于非西歐字符(比如中日韓文等),需要使用native2ascii命令或類似工具將其轉換成ascii碼文件格式,否則會顯示亂碼。
- System.out.println("---Default Locale---");
- ResourceBundle resource = ResourceBundle.getBundle(resourceFile);
- testResourceBundle(resource);
- System.out.println("---Locale.SIMPLIFIED_CHINESE---");
- //創建一個指定Locale(本地化)的ResourceBundle對象,這里指定為Locale.SIMPLIFIED_CHINESE
- //所以ResourceBundle會查找com.test.properties.TestProperties_zh_CN.properties的文件
- //
- //中文相關的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) {
- //取得指定關鍵字的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";
- //創建一個默認的ResourceBundle對象
- //ResourceBundle會查找包com.test.properties下的TestProperties.properties的文件
- //com.test.properties是資源的包名,它跟普通java類的命名規則完全一樣:
- //- 區分大小寫
- //- 擴展名 .properties 省略。就像對于類可以省略掉 .class擴展名一樣
- //- 資源文件必須位于指定包的路徑之下(位于所指定的classpath中)
- //另外,對于非西歐字符(比如中日韓文等),需要使用native2ascii命令或類似工具將其轉換成ascii碼文件格式,否則會顯示亂碼。
- System.out.println("---Default Locale---");
- ResourceBundle resource = ResourceBundle.getBundle(resourceFile);
- testResourceBundle(resource);
- System.out.println("---Locale.SIMPLIFIED_CHINESE---");
- //創建一個指定Locale(本地化)的ResourceBundle對象,這里指定為Locale.SIMPLIFIED_CHINESE
- //所以ResourceBundle會查找com.test.properties.TestProperties_zh_CN.properties的文件
- //
- //中文相關的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) {
- //取得指定關鍵字的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());
- }
- }
- }
解說:
1,為了便于理解,我們把解說放在Java源代碼中了,這里不再詳述了。
2,對于中文資源文件TestProperties_zh_CN.properties,需要使用native2ascii 命令將其轉換為ascii碼。例如:
native2ascii -encoding UTF-8 c:\TestProperties_zh_CN.properties c:\java\com\test\properties\TestProperties_zh_CN.properties
至于native2ascii的詳細用法這里不做詳述了。
3,將上面3個文件都保存在 c:\java\com\test\properties\ 目錄下。其中TestProperties_zh_CN.properties為經過native2ascii轉換后的文件。
4,編譯執行,將會在屏幕上顯示:
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
【編輯推薦】