首頁  >  文章  >  Java  >  Java如何處理properties資源文件

Java如何處理properties資源文件

PHPz
PHPz轉載
2023-04-28 14:19:061082瀏覽

在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

以上是Java如何處理properties資源文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除