文章出處

一、前言                              

  Java工程中想log4j、數據庫連接等配置信息一般都寫在.properties文件中,那么如何讀取這些配置信息呢?下面把相關方法記錄下來供以后查閱。

 

二、.properties文件                        

  配置文件的一種,內容以鍵值對的形式存在,且每個鍵值對獨占一行。#號作為行注釋的起始標志,中文注釋會自動進行unicode編碼。示例:

# ip and port of server socket
ip=127.0.0.1
port=9999
# error message
msg=I'm sorry, bye bye!

  假設上述內容存儲在config.properties文件下,且bin目錄結果如下:

  bin

    |-- main

        |-- Demo.class

    |-- config.properties

   后續章節的示例將以上述內容作為目標對象來操作。

 

三、通過 Properties對象 操作                    

  讀取屬性,示例:

public class Demo{
  public static void main(String[] args){
    Properties props = new Properties();
    
    InputStream in = Demo.class.getResourceAsStream("../config.properties");

    // 或使用文件輸入流(不推薦),假設當前工作目錄為bin
    //InputStream in = new FileInputStream("./config.properties");

    props.load(in);
in.close();
// 讀取特定屬性 String key = "ip"; String ip = props.getProperty(key); // 遍歷所有屬性,方式一 Set keys = props.keySet(); for (Interator it = keys.iterator(); it.hasNext();){ String k = it.next(); System.out.println(k + ":" + props.getProperty(k)); } // 遍歷所有屬性,方式二 Enumeration en = props.propertyNames(); while (en.hasMoreElements()){ String k = en.nextElement(); System.out.println(k + ":" + props.getProperty(k)); } } }

   1. 通過 Demo.class.getResourceAsStream("../config.properties"); 讀取配置文件,配置文件的相對路徑以類文件所在目錄作為當前目錄。

   2. 通過 new FileInputStream("./config.properties"); 讀取配置文件,配置文件的相對路徑以工作目錄(可以通過 System.getProperty("user.dir") 獲取工作目錄)作為當前目錄。

   注意:上述兩種方式獲取的配置文件均沒有被緩存。每次都要重新加載配置文件。

  寫屬性,示例:

Properties props = new Properties();
InputStream in = getClass().getResouceAsStream("properties文件相對于當前類加載路徑的文件目錄");
props.load(in);

OutputStream output = new FileOutputStream("properties文件路徑");
props.setProperty("ip", "10.248.112.123"); // 修改或新增屬性鍵值對
props.store(output, "modify ip value"); // store(OutputStream output, String comment)將修改結果寫入輸出流
output.close()

 

四、通過 ResourceBundle對象 操作                    

  通過該方式僅能讀取配置文件而已,不能進行寫操作。示例:

// ResourceBundle rb = ResourceBundle.getBundle("配置文件相對工程根目錄的相對路徑(不含擴展名)");
ResourceBundle rb = ResourceBundle.getBundle("config");
try{
    String name = rb.getString("name");
}
catch(MissingResourceException ex){

注意:上述方式會緩存配置文件信息,后續讀取時均是讀取緩存中的內容,若在此期間修改了配置內容是無法實時同步的

ResourceBundle有兩個子類ListResourceBundle和PropertyResourceBundle,在讀取properties文件時實際上是使用PropertyResourceBundle來處理。

題外話:

  ResourceBundle主要用于解決國際化和本地化問題。通過資源命名定義各語言和方言的信息,然乎程序在運行時獲取當前本地化信息,并根據本地化信息加載相應的資源完成本地化。

  資源命名規范:

// 僅含家族名
MyResource

// 含家族名和語言
MyResource_en

// 含家族名、語言和國家
MyResource_en_US

  對應的Java代碼:

// ResourceBundle首先會根據語言和國家的本地化信息去查找資源(假設現在要查找MyResource_zh_CN),當找不到時就會找MyResource_zh,再找不到就用MyResource。
ResourceBundle rb = ResourceBundle.getBundle("MyResource", Locale.getDefault())

 

五、總結                              

  當然方式不止這些啦,日后繼續補充!

  尊重原創,轉載請注明來自:http://www.cnblogs.com/fsjohnhuang/p/3995386.html  ^_^肥仔John

 

六、參考                              

http://www.cnblogs.com/panjun-Donet/archive/2009/07/17/1525597.html

 


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()