日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

属性集

發布時間:2025/4/16 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 属性集 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

java.util.Properties集合 extends Hashtable<k,v> implements Map<k,v>
? ? Properties 類表示了一個持久的屬性集。Properties 可保存在流中或從流中加載。
? ? Properties集合是一個唯一和IO流相結合的集合
? ? ? ? 可以使用Properties集合中的方法store,把集合中的臨時數據,持久化寫入到硬盤中存儲
? ? ? ? 可以使用Properties集合中的方法load,把硬盤中保存的文件(鍵值對),讀取到集合中使用

? ? 屬性列表中每個鍵及其對應值都是一個字符串。
? ? ? ? Properties集合是一個雙列集合,key和value默認都是字符串


?使用Properties集合存儲數據,遍歷取出Properties集合中的數據
? ? ? ? Properties集合是一個雙列集合,key和value默認都是字符串
? ? ? ? Properties集合有一些操作字符串的特有方法
? ? ? ? ? ? Object setProperty(String key, String value) 調用 Hashtable 的方法 put。
? ? ? ? ? ? String getProperty(String key) 通過key找到value值,此方法相當于Map集合中的get(key)方法
? ? ? ? ? ? Set<String> stringPropertyNames() 返回此屬性列表中的鍵集,其中該鍵及其對應值是字符串,此方法相當于Map集合中的keySet方法

private static void show01() {//創建Properties集合對象Properties prop = new Properties();//使用setProperty往集合中添加數據prop.setProperty("趙麗穎","168");prop.setProperty("迪麗熱巴","165");prop.setProperty("古力娜扎","160");//prop.put(1,true);//使用stringPropertyNames把Properties集合中的鍵取出,存儲到一個Set集合中Set<String> set = prop.stringPropertyNames();//遍歷Set集合,取出Properties集合的每一個鍵for (String key : set) {//使用getProperty方法通過key獲取valueString value = prop.getProperty(key);System.out.println(key+"="+value);}}

可以使用Properties集合中的方法store,把集合中的臨時數據,持久化寫入到硬盤中存儲
? ? ? ? void store(OutputStream out, String comments)
? ? ? ? void store(Writer writer, String comments)

? ? ? ? 參數:
? ? ? ? ? ? OutputStream out:字節輸出流,不能寫入中文
? ? ? ? ? ? Writer writer:字符輸出流,可以寫中文
? ? ? ? ? ? String comments:注釋,用來解釋說明保存的文件是做什么用的
? ? ? ? ? ? ? ? ? ? 不能使用中文,會產生亂碼,默認是Unicode編碼
? ? ? ? ? ? ? ? ? ? 一般使用""空字符串

? ? ? ? 使用步驟:
? ? ? ? ? ? 1.創建Properties集合對象,添加數據
? ? ? ? ? ? 2.創建字節輸出流/字符輸出流對象,構造方法中綁定要輸出的目的地
? ? ? ? ? ? 3.使用Properties集合中的方法store,把集合中的臨時數據,持久化寫入到硬盤中存儲
? ? ? ? ? ? 4.釋放資源

private static void show02() throws IOException {//1.創建Properties集合對象,添加數據Properties prop = new Properties();prop.setProperty("趙麗穎","168");prop.setProperty("迪麗熱巴","165");prop.setProperty("古力娜扎","160");//2.創建字節輸出流/字符輸出流對象,構造方法中綁定要輸出的目的地FileWriter fw = new FileWriter("09_IOAndProperties\\prop.txt");//3.使用Properties集合中的方法store,把集合中的臨時數據,持久化寫入到硬盤中存儲prop.store(fw,"save data");//4.釋放資源fw.close();prop.store(new FileOutputStream("09_IOAndProperties\\prop2.txt"),"");}

可以使用Properties集合中的方法load,把硬盤中保存的文件(鍵值對),讀取到集合中使用
? ? ? ? void load(InputStream inStream)
? ? ? ? void load(Reader reader)

? ? ? ? 參數:
? ? ? ? ? ? InputStream inStream:字節輸入流,不能讀取含有中文的鍵值對
? ? ? ? ? ? Reader reader:字符輸入流,能讀取含有中文的鍵值對

? ? ? ? 使用步驟:
? ? ? ? ? ? 1.創建Properties集合對象
? ? ? ? ? ? 2.使用Properties集合對象中的方法load讀取保存鍵值對的文件
? ? ? ? ? ? 3.遍歷Properties集合
? ? ? ? 注意:
? ? ? ? ? ? 1.存儲鍵值對的文件中,鍵與值默認的連接符號可以使用=,空格(其他符號)
? ? ? ? ? ? 2.存儲鍵值對的文件中,可以使用#進行注釋,被注釋的鍵值對不會再被讀取
? ? ? ? ? ? 3.存儲鍵值對的文件中,鍵與值默認都是字符串,不用再加引號

private static void show03() throws IOException {//1.創建Properties集合對象Properties prop = new Properties();//2.使用Properties集合對象中的方法load讀取保存鍵值對的文件prop.load(new FileReader("09_IOAndProperties\\prop.txt"));//prop.load(new FileInputStream("09_IOAndProperties\\prop.txt"));//3.遍歷Properties集合Set<String> set = prop.stringPropertyNames();for (String key : set) {String value = prop.getProperty(key);System.out.println(key+"="+value);}}

?

總結

以上是生活随笔為你收集整理的属性集的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。