溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java中怎么處理properties資源文件

發(fā)布時間:2021-07-01 16:25:21 來源:億速云 閱讀:189 作者:Leah 欄目:編程語言

Java中怎么處理properties資源文件,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

在Java語言中,使用一種以.properties為擴展名的文本文件作為資源文件,該類型的文件的內(nèi)容格式為類似:

#注釋語句
some_key=some_value

形式。以#開頭的行作為注釋行,ResourceBundle類處理時會加以忽略;其余的行可以以 key名=value值 的形式加以記述。

Java的ResourceBundle類可以對這種形式的文件加以處理。

ResourceBundle類的使用方法也非常簡單。我們使用一個例子來說明。

我們假設(shè)有下面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名稱,該名稱其實是用于資源文件的本地化處理。什么是本地化呢?我們簡單說明一下:我們在進行系統(tǒng)開發(fā)時,很多時候需要為不同地區(qū)的用戶準備不同的界面,比如,如果一個系統(tǒng)同時面向 英語圈的用戶以及面向中國的用戶,我們就必須為系統(tǒng)準備2套界面(包括消息),一套為英語界面,一套為中文界面。當(dāng)然,除了界面不同之外,系統(tǒng)的處理過程完全一樣。當(dāng)然我們不可能為它們分別開發(fā)2套不同的系統(tǒng),怎么辦呢?這就需要用到資源的本地化處理。也就是說,根據(jù)用戶所處的地區(qū)或語言的不同,分別準備不同的資源文件,這樣就可以為不同的用戶準備不同的界面但使用的卻是同一套系統(tǒng)邏輯。

我們上面的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";     //創(chuàng)建一個默認的ResourceBundle對象     //ResourceBundle會查找包com.test.properties下的TestProperties.properties的文件     //com.test.properties是資源的包名,它跟普通java類的命名規(guī)則完全一樣:     //- 區(qū)分大小寫     //- 擴展名 .properties 省略。就像對于類可以省略掉 .class擴展名一樣     //- 資源文件必須位于指定包的路徑之下(位于所指定的classpath中)     //另外,對于非西歐字符(比如中日韓文等),需要使用native2ascii命令或類似工具將其轉(zhuǎn)換成ascii碼文件格式,否則會顯示亂碼。     System.out.println("---Default Locale---");     ResourceBundle resource = ResourceBundle.getBundle(resourceFile);          testResourceBundle(resource);          System.out.println("---Locale.SIMPLIFIED_CHINESE---");          //創(chuàng)建一個指定Locale(本地化)的ResourceBundle對象,這里指定為Locale.SIMPLIFIED_CHINESE     //所以ResourceBundle會查找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)建一個默認的ResourceBundle對象  //ResourceBundle會查找包com.test.properties下的TestProperties.properties的文件  //com.test.properties是資源的包名,它跟普通java類的命名規(guī)則完全一樣:  //- 區(qū)分大小寫  //- 擴展名 .properties 省略。就像對于類可以省略掉 .class擴展名一樣  //- 資源文件必須位于指定包的路徑之下(位于所指定的classpath中)  //另外,對于非西歐字符(比如中日韓文等),需要使用native2ascii命令或類似工具將其轉(zhuǎn)換成ascii碼文件格式,否則會顯示亂碼。  System.out.println("---Default Locale---");  ResourceBundle resource = ResourceBundle.getBundle(resourceFile);   testResourceBundle(resource);   System.out.println("---Locale.SIMPLIFIED_CHINESE---");   //創(chuàng)建一個指定Locale(本地化)的ResourceBundle對象,這里指定為Locale.SIMPLIFIED_CHINESE  //所以ResourceBundle會查找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());  }      }  }

解說:

1,為了便于理解,我們把解說放在Java源代碼中了,這里不再詳述了。

2,對于中文資源文件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的詳細用法這里不做詳述了。

3,將上面3個文件都保存在 c:\java\com\test\properties\ 目錄下。其中TestProperties_zh_CN.properties為經(jīng)過native2ascii轉(zhuǎn)換后的文件。

4,編譯執(zhí)行,將會在屏幕上顯示:

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

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI