此心光明,亦復何言
在我們平時寫程序的時候,有些參數是經常改變的,而這種改變不是我們預知的。比如說我們開發了一個操作數據庫的模塊,在開發的時候我們連接本地的數據庫那么IP ,數據庫名稱,表名稱,數據庫主機等信息是我們本地的,要使得這個操作數據的模塊具有通用性,那么以上信息就不能寫死在程序里。通常我們的做法是用配置文件來解決。
各種語言都有自己所支持的配置文件,配置文件中很多變量是經常改變的,這樣做也是為了方便用戶,讓用戶能夠脫離程序本身去修改相關的變量設置。像Python支持的配置文件是.ini文件,同樣,它也有自己讀取配置文件的類ConfigParse,方便程序員或用戶通過該類的方法來修改.ini配置文件。在Java中,其配置文件常為.properties文件,格式為文本文件,JDK 內置的java.util.Properties 類 支持.properties 文件的讀寫,為我們操作 .properties 文件提供了便利。
一 .properties 文件的形式
設數據如下:
#以下為服務器、數據庫信息
dbPort = localhost
databaseName = mydb
dbUserName = root
dbPassword = root
#以下為數據庫表信息
dbTable = mytable
#以下為服務器信息
ip = 192.168.0.9
上面的信息我們假設保存在 db.properties 文件中。其中# 開始的一行為注釋信息;在等號“= ”左邊的我們稱之為key ;等號“= ”右邊的我們稱之為value 。(其實就是我們常說的鍵- 值對)key 應該是我們程序中的變量。而value 是我們根據實際情況配置的。
二 Java中Properties類的操作
1 Java Properties類
Java中有個比較重要的類Properties(Java.util.Properties),主要用于讀取Java的配置文件,文件的內容的格式是“鍵=值”的格式,文本注釋信息可以用"#"來注釋。
Properties類繼承自Hashtable,如下:
Properties類提供了幾個主要的方法:
1 load( InputStream inStream )
這個方法可以從 .properties屬性文件 對應的文件輸入流中,加載屬性列表到Properties類對象,即通過對上面的 properties 文件進行裝載來獲取該文件中的所有鍵 - 值對。以供 getProperty ( String key) 來搜索。
如下面的代碼:
Properties pro = new Properties();
FileInputStream in = new FileInputStream("a.properties");
pro.load(in);
in.close();
2 getProperty ( String key )
獲取屬性信息
即用指定的鍵在屬性列表中搜索屬性 也就是通過參數 key 得到 key 所對應的 value
3 setProperty ( String key, String value )
設置屬性信息
即通過調用基類的put方法來設置 鍵 - 值對。
4 store( OutputStream out, String comments )
這個方法將Properties類對象的屬性列表保存到輸出流中
即與 load 方法相反,該方法將鍵 - 值對寫入到指定的文件中去
如下面的代碼:
FileOutputStream oFile = new FileOutputStream(file, "a.properties");
pro.store(oFile, "Comment");
oFile.close();
如果comments不為空,保存后的屬性文件第一行會是#comments,表示注釋信息;如果為空則沒有注釋信息。
注釋信息后面是屬性文件的當前保存時間信息。
5 clear( )
清除所有裝載的 鍵 - 值對。該方法在基類中提供。
load 和 getProperty最常用
有了以上幾個方法我們就可以對.properties 文件進行操作了!
2 Java讀取Properties文件
Java讀取Properties文件的方法有很多,但是最常用的還是通過java.lang.Class類 getResourceAsStream(String name)方法來實現,如下可以這樣調用:
InputStream in = getClass().getResourceAsStream("資源Name");
或者下面這種也常用:
InputStream in = new BufferedInputStream(new FileInputStream(filepath));
3 一個比較綜合的實例
根據key讀取value
讀取properties的全部信息
寫入新的properties信息
//關于Properties類常用的操作
public class TestProperties {
//根據Key讀取Value
public static String GetValueByKey(String filePath, String key) {
Properties pps = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
pps.load(in);
String value = pps.getProperty(key);
System.out.println(key + " = " + value);
return value;
}catch (IOException e) {
e.printStackTrace();
return null;
}
}
//讀取Properties的全部信息
public static void GetAllProperties(String filePath) throws IOException {
Properties pps = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
pps.load(in);
Enumeration en = pps.propertyNames(); //得到配置文件的名字
while(en.hasMoreElements()) {
String strKey = (String) en.nextElement();
String strValue = pps.getProperty(strKey);
System.out.println(strKey + "=" + strValue);
}
}
//寫入Properties信息
public static void WriteProperties (String filePath, String pKey, String pValue) throws IOException {
Properties pps = new Properties();
InputStream in = new FileInputStream(filePath);
//從輸入流中讀取屬性列表(鍵和元素對)
pps.load(in);
//調用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
//強制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調用 put 的結果。
OutputStream out = new FileOutputStream(filePath);
pps.setProperty(pKey, pValue);
//以適合使用 load 方法加載到 Properties 表中的格式,
//將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流
pps.store(out, "Update " + pKey + " name");
}
public static void main(String [] args) throws IOException{
//String value = GetValueByKey("db.properties", "name");
//System.out.println(value);
//GetAllProperties("db.properties");
WriteProperties("db.properties","long", "212");
}
}
結果:
db.properties中文件的數據為:
#Update long name
#Sun Feb 23 18:17:16 CST 2014
name=JJ
Weight=4444
long=212
Height=3333
三 小結
通過上面的例子不難看出,在Java中操作配置文件是非常簡單的。在一個需要用到大量配置信息的模塊或系統里,我們有必要封裝一個專門的類來共使用。通過最后的main函數調用。
世界上所有的追求都是因為熱愛
一枚愛編碼 愛生活 愛分享的IT信徒
— hongXkeX