前言:我們學習java的類一定去看源碼對這個類的介紹,并熟悉一些常用的用法,今天我們就來學學properties這個類。
1.什么是properties或者說properties有什么用?
我們在很多避免硬編碼的應用場景下需要使用properties文件來加載程序需要的配置信息,比如JDBC。Properties類則是properties文件和程序的中間橋梁,不論是從properties文件讀取信息還是寫入信息到properties文件都要經(jīng)由Properties類。
2.Properties類

Properties 類表示了一個持久的屬性集。Properties 可保存在流中或從流中加載。屬性列表中每個鍵及其對應值都是一個字符串。
因為 Properties 繼承于 Hashtable,所以可對 Properties 對象應用 put 和 putAll 方法。但不建議使用這兩個方法,因為它們允許調(diào)用者插入其鍵或值不是 String 的項。相反,應該使用 setProperty 方法。如果在“不安全”的 Properties 對象(即包含非 String 的鍵或值)上調(diào)用 store 或 save 方法,則該調(diào)用將失敗。類似地,如果在“不安全”的 Properties 對象(即包含非 String 的鍵)上調(diào)用 propertyNames 或 list 方法,則該調(diào)用將失敗。
該類是線程安全的
2.1Properties類的方法:

2.1.1 常用方法介紹
-
setProperty
public Object setProperty(String key, String value)
-
getProperty
public String getProperty(String key)
- load
public void load(Reader reader) throws IOException
//按簡單的面向行的格式從輸入字符流中讀取屬性列表(鍵和元素對)
public void load(InputStream inStream) throws IOException
//從輸入流中讀取屬性列表(鍵和元素對)
這里要注意的是鍵和值可以以 : 或者 = 拆分區(qū)域,下面三種方式均可以實現(xiàn)鍵值對的匹配
Truth = Beauty
Truth:Beauty
Truth :Beauty
-
store
public void store(Writer writer, String comments) throws IOException
//以適合使用 load(Reader) 方法的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出字符
?
public void store(OutputStream out, String comments) throws IOException
//以適合使用 load(InputStream) 方法加載到 Properties 表中的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流。
propertyNames
public Enumeration<?> propertyNames()
//返回屬性列表中所有鍵的枚舉,如果在主屬性列表中未找到同名的鍵,則包括默認屬性列表中不同的鍵。
-
stringPropertyNames
public Set<String> stringPropertyNames()
//返回此屬性列表中的鍵集,其中該鍵及其對應值是字符串,如果在主屬性列表中未找到同名的鍵,則還包括默認屬性列表中不同的鍵。其鍵或值不是 String 類型的屬性被忽略。
-
list
public void list(PrintStream out)
//將屬性列表輸出到指定的輸出流,對于調(diào)試有用
public void list(PrintWriter out)
//將屬性列表輸出到指定的輸出流
2.2.2 常用方法使用
import java.io.*;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;
?
public class UseProperty {
public static void main(String[] args) {
Properties prop1 = new Properties();
prop1.setProperty("a", "aaa");
prop1.setProperty("b", "bbb");
prop1.setProperty("c", "ccc");
?
Properties prop2 = new Properties();
prop2.setProperty("x","xxx");
prop2.setProperty("y","yyy");
prop2.setProperty("z","zzz");
?
//使用store方法將prop寫入到properties文件,OutputStream
storePropByOutputStream(prop1);
?
//load方法加載已經(jīng)存在的properties文件,使用InputStream讀取
loadPropByInputStream();
?
//使用store方法將prop寫入到properties文件,Writer
storePropByWriter(prop2);
?
//load方法加載已經(jīng)存在的properties文件,使用Reader讀取
loadPropByReader();
?
//stringPropertyNames()方法
System.out.println("<-------使用方法stringPropertyNames()遍歷Properties列表----->");
Set<String> set = prop2.stringPropertyNames();
for(String key:set){
String value = prop2.getProperty(key);
System.out.println(key+":"+value);
}
?
System.out.println("<-------使用方法propertyNames()遍歷Properties列表----->");
Enumeration enume = prop1.propertyNames();
while(enume.hasMoreElements()){
String key = (String)enume.nextElement();
String value = prop1.getProperty(key);
System.out.println(key+":"+value);
}
?
?
}
?
public static void storePropByOutputStream(Properties prop){
try (OutputStream ops = new FileOutputStream(new File("resources\\a.properties"))) {
prop.store(ops, "config-a");
?
} catch (IOException e) {
e.printStackTrace();
}
}
?
public static void storePropByWriter(Properties prop){
try (Writer writer = new FileWriter(new File("resources\\b.properties"))) {
prop.store(writer, "config-b");
?
} catch (IOException e) {
e.printStackTrace();
}
}
?
public static void loadPropByInputStream(){
try (InputStream ips = new FileInputStream(new File("resources\\a.properties"))) {
Properties prop = new Properties();
prop.load(ips);
Enumeration enumeration = prop.propertyNames();
System.out.println("<-----load by stream----->");
while(enumeration.hasMoreElements()){
String key = (String)enumeration.nextElement();
String value = prop.getProperty(key);
System.out.println(key+":"+value);
}
?
} catch (IOException e) {
e.printStackTrace();
}
}
?
public static void loadPropByReader(){
try (Reader reader = new FileReader(new File("resources\\b.properties"))) {
Properties prop = new Properties();
prop.load(reader);
Enumeration enumeration = prop.propertyNames();
System.out.println("<-----load by reader----->");
while(enumeration.hasMoreElements()){
String key = (String)enumeration.nextElement();
String value = prop.getProperty(key);
System.out.println(key+":"+value);
}
?
} catch (IOException e) {
e.printStackTrace();
}
}
}
運行結果:
<-----load by stream----->
b:bbb
a:aaa
c:ccc
<-----load by reader----->
x:xxx
z:zzz
y:yyy
<-------使用方法stringPropertyNames()遍歷Properties列表----->
x:xxx
z:zzz
y:yyy
<-------使用方法propertyNames()遍歷Properties列表----->
b:bbb
a:aaa
c:ccc