W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
Properties 繼承于 Hashtable.表示一個持久的屬性集.屬性列表中每個鍵及其對應(yīng)值都是一個字符串。
Properties 類被許多Java類使用。例如,在獲取環(huán)境變量時它就作為System.getProperties()方法的返回值。
Properties 定義如下實例變量.這個變量持有一個Properties對象相關(guān)的默認屬性列表。
Properties defaults;
Properties類定義了兩個構(gòu)造方法. 第一個構(gòu)造方法沒有默認值。
Properties()
第二個構(gòu)造方法使用propDefault 作為默認值。兩種情況下,屬性列表都為空:
Properties(Properties propDefault)
除了從Hashtable中所定義的方法,Properties定義了以下方法:
序號 | 方法描述 |
---|---|
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 從輸入流中讀取屬性列表(鍵和元素對)。 |
6 | Enumeration propertyNames( ) 按簡單的面向行的格式從輸入字符流中讀取屬性列表(鍵和元素對)。 |
7 | Object setProperty(String key, String value) 調(diào)用 Hashtable 的方法 put。 |
8 | void store(OutputStream streamOut, String description) 以適合使用 load(InputStream)方法加載到 Properties 表中的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流。 |
下面的程序說明這個數(shù)據(jù)結(jié)構(gòu)支持的幾個方法:
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 + ".");
}
}
以上實例編譯運行結(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號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: