詳解Java程式讀取properties設定檔的方法_java

來源:互聯網
上載者:User

在我們平時寫程式的時候,有些參數是經常改變的,而這種改變不是我們預知的。比如說我們開發了一個操作資料庫的模組,在開發的時候我們串連本地的資料庫那麼IP ,資料庫名稱,表名稱,資料庫主機等資訊是我們本地的,要使得這個操作資料的模組具有通用性,那麼以上資訊就不能寫死在程式裡。通常我們的做法是用設定檔來解決。
各種語言都有自己所支援的設定檔類型。比如Python ,他支援.ini 檔案。因為他內部有一個ConfigParser 類來支援.ini 檔案的讀寫,根據該類提供的方法程式員可以自由的來操作.ini 檔案。而在Java 中,Java 支援的是.properties 檔案的讀寫。JDK 內建的java.util.Properties 類為我們操作.properties 檔案提供了便利。

一..properties 檔案的形式

# 以下為伺服器、資料庫資訊dbPort = localhost databaseName = mydb dbUserName = root dbPassword = root # 以下為資料庫表資訊dbTable = mytable # 以下為伺服器資訊ip = 192.168.0.9 

上面的檔案中我們假設該檔案名稱為:test.properties 檔案。其中# 開始的一行為注釋資訊;在等號“= ”左邊的我們稱之為key ;等號“= ”右邊的我們稱之為value 。(其實就是我們常說的鍵- 值對)key 應該是我們程式中的變數。而value 是我們根據實際情況配置的。

二.JDK 中的Properties 類

Properties 類存在於胞Java.util 中,該類繼承自Hashtable,它提供了幾個主要的方法:
1. getProperty(String key),  用指定的鍵在此屬性列表中搜尋屬性。也就是通過參數key ,得到key 所對應的value 。
2. load(InputStream inStream),從輸入資料流中讀取屬性列表(鍵和元素對)。通過對指定的檔案(比如說上面的    test.properties 檔案)進行裝載來擷取該檔案中的所有鍵- 值對。以供getProperty(String key)來搜尋。
3. setProperty(String key,String value),調用Hashtable的方法put。他通過調用基類的put方法來設值鍵- 值對。
4. store(OutputStream out,String comments),  以適合使用load方法載入到Properties表中的格式,將此Properties表中的屬性列表(鍵和元素對)寫入輸出資料流。與load 方法相反,該方法將鍵- 值對寫入到指定的檔案中去。
5. clear(),清除所有裝載的鍵 - 值對。該方法在基類中提供。
有了以上幾個方法我們就可以對.properties 檔案進行操作了!

三. Java讀取properties檔案樣本
有一個properties檔案box.properties,內容如下:

Color=RedName=BoxLength=18Width=7Heigth=8

擷取其中的屬性值,可用如下代碼:

InputStream in = null;Properties p = new Properties();try {  in = new BufferedInputStream(new FileInputStream("box.properties"));  p.load(in);} catch (FileNotFoundException e) {  // TODO Auto-generated catch block  e.printStackTrace();} catch (IOException e) {  // TODO Auto-generated catch block  e.printStackTrace();}Enumeration<Object> keys = p.keys();while (keys.hasMoreElements()) {  String key = (String) keys.nextElement();  System.out.println(key + ":" + p.getProperty(key));}

或者:

InputStream in;ResourceBundle rb = null;try {  in = new BufferedInputStream(new FileInputStream("box.properties"));  rb = new PropertyResourceBundle(in);} catch (FileNotFoundException e1) {  // TODO Auto-generated catch block  e1.printStackTrace();} catch (IOException e) {  // TODO Auto-generated catch block  e.printStackTrace();}if (rb != null) {  Enumeration<String> keys = rb.getKeys();  while (keys.hasMoreElements()) {    String key = (String) keys.nextElement();    System.out.println(key + ":" + rb.getString(key));  }}

 不過輸出順序與原始檔案不同。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.