利用 Java 的 Properties 類讀取配置文件信息
在我們平時寫程序的時候,有些參數是經常改變的,而這種改變不是我們預知的。比如說我們開發了一個操作數據庫的模塊,在開發的時候我們連接本地的數據庫那么 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 文件進行操作了!
三.代碼實例
package configuration;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;


/** *//** *//** *//**
* 讀取properties文件
* @author Qutr
*
*/
public class Configuration



{
private Properties propertie;
private FileInputStream inputFile;
private FileOutputStream outputFile;

/** *//** *//** *//**
* 初始化Configuration類
*/
public Configuration()


{
propertie = new Properties();
}

/** *//** *//** *//**
* 初始化Configuration類
* @param filePath 要讀取的配置文件的路徑+名稱
*/
public Configuration(String filePath)


{
propertie = new Properties();

try 
{
inputFile = new FileInputStream(filePath);
propertie.load(inputFile);
inputFile.close();

} catch (FileNotFoundException ex) 
{
System.out.println("讀取屬性文件--->失敗!- 原因:文件路徑錯誤或者文件不存在");
ex.printStackTrace();

} catch (IOException ex) 
{
System.out.println("裝載文件--->失敗!");
ex.printStackTrace();
}
}//end ReadConfigInfo(
)

/** *//** *//** *//**
* 重載函數,得到key的值
* @param key 取得其值的鍵
* @return key的值
*/
public String getValue(String key)


{

if(propertie.containsKey(key))
{
String value = propertie.getProperty(key);//得到某一屬性的值
return value;
}
else
return "";
}//end getValue(
)

/** *//** *//** *//**
* 重載函數,得到key的值
* @param fileName properties文件的路徑+文件名
* @param key 取得其值的鍵
* @return key的值
*/
public String getValue(String fileName, String key)


{

try 
{
String value = "";
inputFile = new FileInputStream(fileName);
propertie.load(inputFile);
inputFile.close();

if(propertie.containsKey(key))
{
value = propertie.getProperty(key);
return value;
}else
return value;

} catch (FileNotFoundException e) 
{
e.printStackTrace();
return "";

} catch (IOException e) 
{
e.printStackTrace();
return "";

} catch (Exception ex) 
{
ex.printStackTrace();
return "";
}
}//end getValue(
)

/** *//** *//** *//**
* 清除properties文件中所有的key和其值
*/
public void clear()


{
propertie.clear();
}//end clear();

/** *//** *//** *//**
* 改變或添加一個key的值,當key存在于properties文件中時該key的值被value所代替,
* 當key不存在時,該key的值是value
* @param key 要存入的鍵
* @param value 要存入的值
*/
public void setValue(String key, String value)


{
propertie.setProperty(key, value);
}//end setValue(
)

/** *//** *//** *//**
* 將更改后的文件數據存入指定的文件中,該文件可以事先不存在。
* @param fileName 文件路徑+文件名稱
* @param description 對該文件的描述
*/
public void saveFile(String fileName, String description)


{

try 
{
outputFile = new FileOutputStream(fileName);
propertie.store(outputFile, description);
outputFile.close();

} catch (FileNotFoundException e) 
{
e.printStackTrace();

} catch (IOException ioe)
{
ioe.printStackTrace();
}
}//end saveFile(
)
public static void main(String[] args)


{
Configuration rc = new Configuration(".\config\test.properties");//相對路徑
String ip = rc.getValue("ipp");//以下讀取properties文件的值
String host = rc.getValue("host");
String tab = rc.getValue("tab");
System.out.println("ip = " + ip + "ip-test leng = " + "ip-test".length());//以下輸出properties讀出的值
System.out.println("ip's length = " + ip.length());
System.out.println("host = " + host);
System.out.println("tab = " + tab);

Configuration cf = new Configuration();
String ipp = cf.getValue(".\config\test.properties", "ip");
System.out.println("ipp = " + ipp);
// cf.clear();
cf.setValue("min", "999");
cf.setValue("max", "1000");
cf.saveFile(".\config\save.perperties", "test");
// Configuration saveCf = new Configuration();
// saveCf.setValue("min", "10");
// saveCf.setValue("max", "1000");
// saveCf.saveFile(".\config\save.perperties");
}//end main()
}//end class ReadConfigInfo


四.小結
通過上面的例子不難看出,在Java中操作配置文件是非常簡單的。在一個需要用到大量配置信息的模塊或系統里,我們有必要封裝一個專門的類來共使用。通過最后的main函數調用,相信大家可以看出該類的用法。不足指出希望大家多多指點。
原文:http://mop9.blog.sohu.com/18009877.html
posted on 2007-11-12 14:07
lk 閱讀(658)
評論(0) 編輯 收藏 所屬分類:
j2se 、
j2ee