W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
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)造方法沒(méi)有默認(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ì))寫(xiě)入輸出流。 |
下面的程序說(shuō)明這個(gè)數(shù)據(jù)結(jié)構(gòu)支持的幾個(gè)方法:
import java.util.*;
public class PropDemo {
public static void main(String args[]) {
Properties capitals = new Properties();
Set states;
String str;
capitals.put("Illinois", "Springfield");
capitals.put("Missouri", "Jefferson City");
capitals.put("Washington", "Olympia");
capitals.put("California", "Sacramento");
capitals.put("Indiana", "Indianapolis");
// Show all states and capitals in hashtable.
states = capitals.keySet(); // get set-view of keys
Iterator itr = states.iterator();
while(itr.hasNext()) {
str = (String) itr.next();
System.out.println("The capital of " +
str + " is " + capitals.getProperty(str) + ".");
}
System.out.println();
// look for state not in list -- specify default
str = capitals.getProperty("Florida", "Not Found");
System.out.println("The capital of Florida is "
+ str + ".");
}
}
以上實(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.
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: