java.util.Properties 集合 extends Hashtable<k,v>implements Map<k,v>
* 持久的屬性集 Propertis可以保存在流中 或者從流中加載
* 唯一的一個和IO流相結合的集合
* store 把流中臨時的數據 持久化到硬盤中存儲
* load把硬盤中的文件(鍵值對) 讀取到 集合中使用
一:使用properties集合存儲數據,遍歷出來
方法:
1.propertes 集合有一些操作字符串的方法
2.setProperties(String key,Strign value)
3.getProperties(String key);
4.stringPropertyNames();----->keySet方法
public void test01(){
Properties pro = new Properties();
pro.setProperty("ruirui","211");
pro.setProperty("haohao","985");
pro.setProperty("guoguo","222");
Set<String> s = pro.stringPropertyNames();
for(String key:s){
String value = pro.getProperty(key);
System.out.println(key+" "+value);
}
}
二:把集合中的臨時數據寫到硬盤上
store 把流中臨時的數據 持久化到硬盤中存儲
load把硬盤中的文件(鍵值對) 讀取到 集合中使用
public void test02() throws IOException {
Properties pro = new Properties();
pro.setProperty("ruirui","211");
pro.setProperty("haohao","985");
pro.setProperty("guoguo","222");
//1 創建字節輸出流 //字符輸出流 構造方法中要綁定輸出的目的地
//第一種方法
// FileWriter fw = new FileWriter("d:\\a.txt");
// pro.store(fw,"save data");
// fw.close();
//第二種方法
pro.store(new FileWriter("d:\\a.txt"),"");
}
三:使用properties集合中的方法,load 把硬盤中的文件(鍵值對)讀取到集合中使用
public void test03() throws IOException {
// 1 創建集合
Properties po = new Properties();
// 2 load方法讀取數據 並保存到對應的集合中
po.load(new FileReader("d:\\a.txt"));
//3 遍歷集合po
Set<String> s = po.stringPropertyNames();
for(String key:s){
String value = po.getProperty(key);
System.out.println(key+"="+value);
}
}