以前寫連接池的時(shí)候就用過(guò)Properties,就知道怎么用,但是不是很明白它都做什么用!今天偶的空閑,在網(wǎng)上轉(zhuǎn)了轉(zhuǎn),收益不小,讓我對(duì)它有了進(jìn)一步的認(rèn)識(shí)。熟悉的就不用看了。
如果不熟悉 java.util.Properties 類,那么現(xiàn)在告訴您它是用來(lái)在一個(gè)文件中存儲(chǔ)鍵-值對(duì)的,其中鍵和值是用等號(hào)分隔的。(如清單 1 所示)。最近更新的 java.util.Properties 類現(xiàn)在提供了一種為程序裝載和存儲(chǔ)設(shè)置的更容易的方法: loadFromXML(InputStream is) 和 storeToXML(OutputStream os, String comment) 方法。
一下是詳細(xì)的說(shuō)明,希望能給大家?guī)?lái)幫助。
清單 1. 一組屬性示例
foo=bar
fu=baz
將清單 1 裝載到 Properties 對(duì)象中后,您就可以找到兩個(gè)鍵( foo 和 fu )和兩個(gè)值( foo 的 bar 和 fu 的 baz )了。這個(gè)類支持帶 \u 的嵌入 Unicode 字符串,但是這里重要的是每一項(xiàng)內(nèi)容都當(dāng)作 String 。
清單 2 顯示了如何裝載屬性文件并列出它當(dāng)前的一組鍵和值。只需傳遞這個(gè)文件的 InputStream 給 load() 方法,就會(huì)將每一個(gè)鍵-值對(duì)添加到 Properties 實(shí)例中。然后用 list() 列出所有屬性或者用 getProperty() 獲取單獨(dú)的屬性。
清單 2. 裝載屬性
import java.util.*;
import java.io.*;
public class LoadSample {
public static void main(String args[]) throws Exception {
Properties prop = new Properties();
FileInputStream fis =
new FileInputStream("sample.properties");
prop.load(fis);
prop.list(System.out);
System.out.println("\nThe foo property: " +
prop.getProperty("foo"));
}
}
運(yùn)行 LoadSample 程序生成如清單 3 所示的輸出。注意 list() 方法的輸出中鍵-值對(duì)的順序與它們?cè)谳斎胛募械捻樞虿灰粯印?Properties 類在一個(gè)散列表(hashtable,事實(shí)上是一個(gè) Hashtable 子類)中儲(chǔ)存一組鍵-值對(duì),所以不能保證順序。
清單 3. LoadSample 的輸出
-- listing properties --
fu=baz
foo=bar
The foo property: bar
XML 屬性文件
這里沒有什么新內(nèi)容。 Properties 類總是這樣工作的。不過(guò),新的地方是從一個(gè) XML 文件中裝載一組屬性。它的 DTD 如清單 4 所示。
清單 4. 屬性 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>
如果不想細(xì)讀 XML DTD,那么可以告訴您它其實(shí)就是說(shuō)在外圍 <properties> 標(biāo)簽中包裝的是一個(gè) <comment> 標(biāo)簽,后面是任意數(shù)量的 <entry> 標(biāo)簽。對(duì)每一個(gè) <entry> 標(biāo)簽,有一個(gè)鍵屬性,輸入的內(nèi)容就是它的值。清單 5 顯示了 清單 1中的屬性文件的 XML 版本是什么樣子的。
清單 5. XML 版本的屬性文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Hi</comment>
<entry key="foo">bar</entry>
<entry key="fu">baz</entry>
</properties>
如果清單 6 所示,讀取 XML 版本的 Properties 文件與讀取老格式的文件沒什么不同。
清單 6. 讀取 XML Properties 文件
import java.util.*;
import java.io.*;
public class LoadSampleXML {
public static void main(String args[]) throws Exception {
Properties prop = new Properties();
FileInputStream fis =
new FileInputStream("sampleprops.xml");
prop.loadFromXML(fis);
prop.list(System.out);
System.out.println("\nThe foo property: " +
prop.getProperty("foo"));
}
}
關(guān)于資源綁定的說(shuō)明
雖然 java.util.Properties 類現(xiàn)在除了支持鍵-值對(duì),還支持屬性文件作為 XML 文件,不幸的是,沒有內(nèi)置的選項(xiàng)可以將 ResourceBundle 作為一個(gè) XML 文件處理。是的, PropertyResourceBundle 不使用 Properties 對(duì)象來(lái)裝載綁定,不過(guò)裝載方法的使用是硬編碼到類中的,而不使用較新的 loadFromXML() 方法。
運(yùn)行清單 6 中的程序產(chǎn)生與原來(lái)的程序相同的輸出,如 清單 2所示。
保存 XML 屬性
新的 Properties 還有一個(gè)功能是將屬性存儲(chǔ)到 XML 格式的文件中。雖然 store() 方法仍然會(huì)創(chuàng)建一個(gè)類似 清單 1 所示的文件,但是現(xiàn)在可以用新的 storeToXML() 方法創(chuàng)建如 清單 5 所示的文件。只要傳遞一個(gè) OutputStream 和一個(gè)用于注釋的 String 就可以了。清單 7 展示了新的 storeToXML() 方法。
清單 7. 將 Properties 存儲(chǔ)為 XML 文件
import java.util.*;
import java.io.*;
public class StoreXML {
public static void main(String args[]) throws Exception {
Properties prop = new Properties();
prop.setProperty("one-two", "buckle my shoe");
prop.setProperty("three-four", "shut the door");
prop.setProperty("five-six", "pick up sticks");
prop.setProperty("seven-eight", "lay them straight");
prop.setProperty("nine-ten", "a big, fat hen");
FileOutputStream fos =
new FileOutputStream("rhyme.xml");
prop.storeToXML(fos, "Rhyme");
fos.close();
}
}
運(yùn)行清單 7 中的程序產(chǎn)生的輸出如清單 8 所示。
清單 8. 存儲(chǔ)的 XML 文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Rhyme</comment>
<entry key="seven-eight">lay them straight</entry>
<entry key="five-six">pick up sticks</entry>
<entry key="nine-ten">a big, fat hen</entry>
<entry key="three-four">shut the door</entry>
<entry key="one-two">buckle my shoe</entry>
</properties>
在這里改了一個(gè)例子:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 實(shí)現(xiàn)properties文件的讀取
* @author haoxuewu
*/
public class Test {
public static void main(String[] args) {
try {
long start = System.currentTimeMillis();
InputStream is = new FileInputStream("conf.properties");
Properties p = new Properties();
p.load(is);
is.close();
System.out.println("SIZE : " + p.size());
System.out.println("homepage : " + p.getProperty("homepage"));
System.out.println("author : " + p.getProperty("author"));
System.out.println("school : " + p.getProperty("school"));
long end = System.currentTimeMillis();
System.out.println("Cost : " + (end - start));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
conf.properties
# Configuration file
homepage = http://www.aygfsteel.com/haoxuewu
author = bbflyerwww
school = jilinjianzhugongchengxueyuan
Result
SIZE:3
homepage : http://www.aygfsteel.com/haoxuewu
author : bbflyerwww
school : jilinjianzhugongchengxueyuan
Cost : 0