1、Properties的基本介紹
Properties類是一個(gè)Map集合粱锐,表示了一個(gè)持久的屬性集公黑。Properties可保存在流中或從流中加載涩维。屬性列表中每個(gè)鍵及其對(duì)應(yīng)值都是一個(gè)字符串,即屬性名和屬性值都必須是字符串類型的翔冀,所以這個(gè)集合類并沒(méi)有使用泛型导街。
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
public class Demo1 {
public static void main(String[] args) {
fun();
}
public static void fun() {
// 創(chuàng)建Properties集合類對(duì)象
Properties pro = new Properties();
// 向集合中添加屬性鍵值對(duì)
pro.setProperty("name", "Tom");
pro.setProperty("age", "20");
// 得到所有鍵的集合,相當(dāng)于Map集合的keySet()方法
Set<String> keys = pro.stringPropertyNames();
// 創(chuàng)建迭代器
Iterator<String> ite = keys.iterator();
while (ite.hasNext()) {
// 得到所有的鍵
String key = ite.next();
// 根據(jù)鍵來(lái)獲取值
String value = pro.getProperty(key);
System.out.println(key + "=" + value);
}
// 修改集合中已存在的鍵值對(duì)
/*
* 因?yàn)镸ap集合中的鍵不能相同
* 相同的話纤子,后者會(huì)覆蓋前者
* 因此創(chuàng)建同名的鍵也就相當(dāng)于修改
* 但集合已經(jīng)在內(nèi)存中
* 再次修改相當(dāng)于在內(nèi)存中做的修改
* 屬于臨時(shí)性的搬瑰,是無(wú)法保存的
*/
pro.setProperty("name", "Mike");
// 再次迭代查看集合是否改變
Set<String> keys2 = pro.stringPropertyNames();
Iterator<String> ite2 = keys2.iterator();
while (ite2.hasNext()) {
String key = ite2.next();
String value = pro.getProperty(key);
System.out.println(key + "=" + value);
}
}
}
2、Properties的系統(tǒng)屬性集(僅了解)
系統(tǒng)屬性集是在System類下的一個(gè)方法控硼,目的是用來(lái)確定當(dāng)前的系統(tǒng)屬性泽论,將使用的當(dāng)前系統(tǒng)屬性集合作為Properties對(duì)象返回。
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
public class Demo2 {
public static void main(String[] args) {
fun();
}
public static void fun(){
//得到系統(tǒng)屬性集
Properties pro = System.getProperties();
//遍歷集合
Set<String> keys = pro.stringPropertyNames();
Iterator<String> ite = keys.iterator();
while(ite.hasNext()){
String key = ite.next();
String value = pro.getProperty(key);
System.out.println(key+"="+value);
}
}
}
對(duì)上述代碼的遍歷部分可采用一個(gè)更加簡(jiǎn)便的list()方法卡乾,該方法對(duì)調(diào)試很有用翼悴。
import java.util.Properties;
public class Demo3 {
public static void main(String[] args) {
fun();
}
public static void fun(){
Properties pro = System.getProperties();
//把集合中的鍵值對(duì)寫入控制臺(tái)
pro.list(System.out);
}
}
3、使用Properties模擬設(shè)置文件
首先創(chuàng)建文件comfig.properties说订,此處用properties僅做講解使用抄瓦,現(xiàn)在都是用xml格式的文件當(dāng)做設(shè)置文件,雖然properties格式過(guò)時(shí)陶冷,但意義相通钙姊;然后在文件內(nèi)部編寫屬性,格式固定埂伦,中間是一個(gè)"="煞额,左邊是鍵,右邊是值,例如:
color=red
之后對(duì)文件進(jìn)行操作膊毁,實(shí)現(xiàn)對(duì)設(shè)置修改及存儲(chǔ)胀莹。
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class Demo3 {
public static void main(String[] args) throws IOException {
fun();
}
public static void fun() throws IOException {
// 創(chuàng)建Properties集合類對(duì)象
Properties pro = new Properties();
// 創(chuàng)建讀取文件的字符讀取流
FileReader fr = new FileReader("F:\\comfig.properties");
// 把文件中的鍵值對(duì)存儲(chǔ)到集合中
pro.load(fr);
// 修改顏色,此時(shí)還是在內(nèi)存中做修改婚温,文件內(nèi)容并未發(fā)生變化
pro.setProperty("color", "green");
// 把內(nèi)存中的修改儲(chǔ)存到文件中
FileWriter fw = new FileWriter("F:\\comfig.properties");
// 把集合中的鍵值對(duì)寫入到文件中
pro.store(fw, null);
/*
* store()方法中后一個(gè)參數(shù)其實(shí)是一個(gè)字符串類型的注釋
* 如果不需要注釋就寫null
* 同時(shí)不論是否添加注釋
* 在修改后的文件中都會(huì)再添加一個(gè)注釋行
* 表明做出此次修改的時(shí)間
* 例如:
* #Wed Mar 15 14:55:49 CST 2017
* color=green
*/
}
}
這樣操作之后描焰,修改的內(nèi)容就被保存到了文件也就是硬盤中,代表設(shè)置也已經(jīng)存儲(chǔ)了栅螟。下次再打開程序時(shí)荆秦,會(huì)先讀取文件,也就表示載入了修改后的設(shè)置力图,實(shí)現(xiàn)了文件的存儲(chǔ)步绸。
版權(quán)聲明:歡迎轉(zhuǎn)載,歡迎擴(kuò)散吃媒,但轉(zhuǎn)載時(shí)請(qǐng)標(biāo)明作者以及原文出處瓤介,謝謝合作! ↓↓↓