使用Properties集合存储数据,遍历取出Properties集合中的数据
生活随笔
收集整理的這篇文章主要介紹了
使用Properties集合存储数据,遍历取出Properties集合中的数据
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
package com.learn.demo07.Prop;import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;/*java.util.Properties集合 extends Hashtable<k,v> implements Map<k,v>Properties 類表示了一個(gè)持久的屬性集。Properties 可保存在流中或從流中加載。Properties集合是一個(gè)唯一和IO流相結(jié)合的集合可以使用Properties集合中的方法store,把集合中的臨時(shí)數(shù)據(jù),持久化寫入到硬盤中存儲(chǔ)可以使用Properties集合中的方法load,把硬盤中保存的文件(鍵值對),讀取到集合中使用屬性列表中每個(gè)鍵及其對應(yīng)值都是一個(gè)字符串。Properties集合是一個(gè)雙列集合,key和value默認(rèn)都是字符串*/
public class Demo01Properties {public static void main(String[] args) throws IOException {show01();}/*使用Properties集合存儲(chǔ)數(shù)據(jù),遍歷取出Properties集合中的數(shù)據(jù)Properties集合是一個(gè)雙列集合,key和value默認(rèn)都是字符串Properties集合有一些操作字符串的特有方法Object setProperty(String key, String value) 調(diào)用 Hashtable 的方法 put。String getProperty(String key) 通過key找到value值,此方法相當(dāng)于Map集合中的get(key)方法Set<String> stringPropertyNames() 返回此屬性列表中的鍵集,其中該鍵及其對應(yīng)值是字符串,此方法相當(dāng)于Map集合中的keySet方法*/private static void show01() {//創(chuàng)建Properties集合對象Properties prop = new Properties();//使用setProperty往集合中添加數(shù)據(jù)prop.setProperty("趙麗穎","168");prop.setProperty("迪麗熱巴","165");prop.setProperty("古力娜扎","160");//prop.put(1,true);//使用stringPropertyNames把Properties集合中的鍵取出,存儲(chǔ)到一個(gè)Set集合中Set<String> set = prop.stringPropertyNames();//遍歷Set集合,取出Properties集合的每一個(gè)鍵for (String key : set) {//使用getProperty方法通過key獲取valueString value = prop.getProperty(key);System.out.println(key+"="+value);}}
}
?
總結(jié)
以上是生活随笔為你收集整理的使用Properties集合存储数据,遍历取出Properties集合中的数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JDK7和JDK9流中异常的处理
- 下一篇: 缓冲流的原理