Java中使用ResourceBundle訪問資源檔(properties檔案)

來源:互聯網
上載者:User

在程式中經常需要使用常量,如果直接寫在程式裡,改變這些字串時必須重新編譯,例如編寫資料庫應用的時候資料庫驅動程式、URL、使用者名稱和口令等資訊,可以使用資源檔(Properties檔案),Java中提供了 ResourceBundle類來方便對屬性檔案的訪問。本文介紹如何編寫properties檔案,如何訪問,如何?國際化。

1、properties檔案的編寫  

檔案的尾碼名為.properties。

檔案中每一行的格式為:key=value

例如:

database.driver=com.mysql.jdbc.Drvier

database.url=jdbc:mysql://localhost:3306:test

database.user=root

database.pass=root

如果要是使用注釋可以使用#,例如:

#如果採用其他資料庫,需要修改這些資訊

2、使用ResourceBundl解析資源檔

分兩步:載入資源檔,然後擷取某個特定資訊。

2.1 載入資源檔

使用getBundle方法,參數是資源檔的

ResourceBundle resource = ResourceBundle.getBundle("messages");

參數為資源檔的名字,注意不用謝尾碼名。

2.2擷取資源檔中的資訊

使用getString方法,例如要擷取上面寫的驅動程式,可以使用下面的代碼:

String driverName = resource.getString("database.driver");

3、執行個體

3.1 資源檔

檔案名稱:database.properties

檔案內容:

database.driver=com.mysql.jdbc.Drvier
database.url=jdbc:mysql://localhost:3306:test
database.user=root
database.pass=root

3.2 讀取資源檔的Java檔案

package ch6;

import java.util.ResourceBundle;

public class ResourceBundleTest {

 public static void main(String[] args) {
  ResourceBundle resource = ResourceBundle.getBundle("ch6.database");
  String driverName = resource.getString("database.driver");
  String url = resource.getString("database.url");
  String user = resource.getString("database.user");
  String pass = resource.getString("database.pass");
  
  System.out.println("驅動程式:"+driverName);
  System.out.println("URL:"+driverName);
  System.out.println("使用者名稱:"+driverName);
  System.out.println("口令:"+driverName);
 }
}

3.3 執行結果

驅動程式:com.mysql.jdbc.Drvier
URL:com.mysql.jdbc.Drvier
使用者名稱:com.mysql.jdbc.Drvier
口令:com.mysql.jdbc.Drvier

4、使用Locale設定國家和地區支援國際化

有些應用需要同時支援多國語言,這時候如果為每種語言都開發一套應用將很費力,並且很難維護,好的方法是不用應用中的不用語言表示的資訊單獨儲存在資源檔中,而把不變的邏輯內容保留在應用中,需要的時候從資源檔中載入可變的內容。國際化就是來解決這個問題的。把不同語言的資訊寫在不同的檔案中。根據需要調用相應的檔案。

4.1 編寫支援多語言的資源檔。

預設檔案:message.properties

login.user=Username

login.pass=Password

login.submit=Submit

簡體中文:message_zh_CN.properties

login.user=使用者名稱

login.pass=口令

login.submit=提交

注意:中文資源檔需要使用native2ascii處理,轉換為Unicode編碼。

4.2 調用不同的資源檔

使用ResourceBundle的getBundle方法,第一個參數值資源檔的名字,第二個參數是國家和地區。例如:

ResourceBundle.getBundle("ch6.message", Locale.SIMPLIFIED_CHINESE);

注意:Locale中定義了很多表示不同語言的常量。也可以直接指出地區和語言。

Locale bLocale = new Locale("en", "US");
Locale cLocale = new Locale("en", "GB");

第一個參數是語言代碼,第二個參數是國家代碼。

語言如下:

Language Code Description
de German
en English
fr French
ja Japanese
jw Javanese
ko Korean
zh Chinese

國家代碼如下:

 

Country Code Description
CN China
DE Germany
FR France
IN India
US United States

結束!

李緒成 CSDN Blog:http://blog.csdn.net/javaeeteacher
CSDN學生大本營:http://student.csdn.net/space.php?uid=124362
如果喜歡我的文章,就加我為好友:http://student.csdn.net/invite.php?u=124362&c=7be8ba2b6f3b6cc5

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.