很多時(shí)候,我們會(huì)把一些設(shè)置,環(huán)境等數(shù)據(jù)量不大,而且不經(jīng)常改變的參數(shù)寫到配置文件中,然后在程序中讀取這些參數(shù)。
讀取參數(shù)是一個(gè)很簡單的過程,可以用java.util.Properties來完成。先來看看官方文檔:
Properties 類表示了一個(gè)持久的屬性集。Properties 可保存在流中或從流中加載。屬性列表中每個(gè)鍵及其對(duì)應(yīng)值都是一個(gè)字符串。
一個(gè)屬性列表可包含另一個(gè)屬性列表作為它的“默認(rèn)值”;如果未能在原有的屬性列表中搜索到屬性鍵,則搜索第二個(gè)屬性列表。
因?yàn)?nbsp;Properties 繼承于 Hashtable,所以可對(duì) Properties 對(duì)象應(yīng)用 put 和 putAll 方法。但強(qiáng)烈反對(duì)使用這兩個(gè)方法,因?yàn)樗鼈冊(cè)试S調(diào)用方插入其鍵或值不是 Strings 的項(xiàng)。相反,應(yīng)該使用 setProperty 方法。如果在“有危險(xiǎn)”的 Properties 對(duì)象(即包含非 String 的鍵或值)上調(diào)用 store 或 save 方法,則該調(diào)用將失敗。
load 和 store 方法按下面所指定的、簡單的面向行的格式加載和存儲(chǔ)屬性。此格式使用 ISO 8859-1 字符編碼。可以使用 Unicode 轉(zhuǎn)義符來編寫此編碼中無法直接表示的字符;轉(zhuǎn)義序列中只允許單個(gè) 'u' 字符。可使用 native2ascii 工具對(duì)屬性文件和其他字符編碼進(jìn)行相互轉(zhuǎn)換。
loadFromXML(InputStream) 和 storeToXML(OutputStream, String, String) 方法按簡單的 XML 格式加載和存儲(chǔ)屬性。默認(rèn)使用 UTF-8 字符編碼,但如果需要,可以指定某種特定的編碼。XML 屬性文檔具有以下 DOCTYPE 聲明:
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
注意,導(dǎo)入或?qū)С鰧傩詴r(shí)不 訪問系統(tǒng) URI (http://java.sun.com/dtd/properties.dtd);該系統(tǒng) URI 僅作為一個(gè)惟一標(biāo)識(shí) DTD 的字符串:
<?xml version="1.0" encoding="UTF-8"?>
<!-- DTD for properties -->
<!ELEMENT properties ( comment?, entry* ) >
<!ATTLIST properties version CDATA #FIXED "1.0">
<!ELEMENT comment (#PCDATA) >
<!ELEMENT entry (#PCDATA) >
<!ATTLIST entry key CDATA #REQUIRED>
寫一個(gè)讀取參數(shù)的簡單的例子:
package woxingwosu;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.Properties;
public class ReadProperties {
public static void main(String[] args) {
//對(duì)于XML文件一定要符合http://java.sun.com/dtd/properties.dtd
String name="woxingwosu.xml";
Properties prop=loadFile(name);
if(prop!=null){
System.out.println("XMl file");
System.out.println("no="+prop.getProperty("no"));
System.out.println("name="+prop.getProperty("name"));
}
//對(duì)于非XML文件,key與value之間可以用冒號(hào)隔開,也可以用空格隔開
name="woxingwosu.properties";
prop=loadFile(name);
if(prop!=null){
System.out.println("Properties file");
System.out.println("no="+prop.getProperty("no"));
System.out.println("name="+prop.getProperty("name"));
}
}
/**
* 加載配置文件
* @param path --文件的路徑
* @param isXmlFile --true:配置文件是XML文件,false:非XML文件
* @return(Properties)
*/
private static Properties loadFile(String path){
Properties prop=null;
try{
BufferedInputStream inBuff=new BufferedInputStream(new FileInputStream(path));
prop=new Properties();
if(path.endsWith(".xml"))
prop.loadFromXML(inBuff);
else
prop.load(inBuff);
inBuff.close();
}catch(Exception e){
e.printStackTrace();
}
return prop;
}
}
配置文件woxingwosu.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="no">woxingwosu</entry>
<entry key="name">我行我素</entry>
</properties>
配置文件woxingwosu.properties(我用Unicode編碼)
no:woxingwosu
name \u6211\u884c\u6211\u7d20
除了讀取參數(shù),同時(shí)也可以修改參數(shù),添加參數(shù),或者生成一個(gè)文件,參考一下幫助文檔,其實(shí)都很簡單。
posted on 2007-07-23 18:34
破繭而出 閱讀(551)
評(píng)論(0) 編輯 收藏 所屬分類:
Java