Java 教程
Properties 繼承于 Hashtable.表示一個(gè)持久的屬性集.屬性列表中每個(gè)鍵及其對(duì)應(yīng)值都是一個(gè)字符串。
Properties 類被許多Java類使用。例如,在獲取環(huán)境變量時(shí)它就作為System.getProperties()方法的返回值。
Properties 定義如下實(shí)例變量.這個(gè)變量持有一個(gè)Properties對(duì)象相關(guān)的默認(rèn)屬性列表。
Properties defaults;
Properties類定義了兩個(gè)構(gòu)造方法. 第一個(gè)構(gòu)造方法沒有默認(rèn)值。
Properties()
第二個(gè)構(gòu)造方法使用propDefault 作為默認(rèn)值。兩種情況下,屬性列表都為空:
Properties(Properties propDefault)
除了從Hashtable中所定義的方法,Properties定義了以下方法:
序號(hào) | 方法描述 |
---|---|
1 |
String getProperty(String key) ?用指定的鍵在此屬性列表中搜索屬性。 |
2 |
String getProperty(String key, String defaultProperty) 用指定的鍵在屬性列表中搜索屬性。 |
3 |
void list(PrintStream streamOut) ?將屬性列表輸出到指定的輸出流。 |
4 |
void list(PrintWriter streamOut) 將屬性列表輸出到指定的輸出流。 |
5 |
void load(InputStream streamIn) throws IOException ?從輸入流中讀取屬性列表(鍵和元素對(duì))。 |
6 |
Enumeration propertyNames( ) 按簡(jiǎn)單的面向行的格式從輸入字符流中讀取屬性列表(鍵和元素對(duì))。 |
7 |
Object setProperty(String key, String value) ?調(diào)用?Hashtable?的方法?put。 |
8 |
void store(OutputStream streamOut, String description) ?以適合使用??load(InputStream)方法加載到?Properties?表中的格式,將此?Properties?表中的屬性列表(鍵和元素對(duì))寫入輸出流。 |
下面的程序說明這個(gè)數(shù)據(jù)結(jié)構(gòu)支持的幾個(gè)方法:
以上實(shí)例編譯運(yùn)行結(jié)果如下:
The capital of Missouri is Jefferson City. The capital of Illinois is Springfield. The capital of Indiana is Indianapolis. The capital of California is Sacramento. The capital of Washington is Olympia. The capital of Florida is Not Found.