Properties集合整理

2021-06-22 17:17:52 字數 1883 閱讀 9325

properties集合特點:

1、該集合的鍵和值都是字串型別

2、集合中的資料可以儲存到流中或從流中獲取資料

properties常用方法

//儲存元素

prop.setproperty("zhangsan", "20");

prop.setproperty("wangwu","25");

prop.setproperty("lisi", "29");

prop.setproperty("zhaoliu", "30");

//修改元素

prop.setproperty("zhangsan", "19");

//取出所有元素

setnames=prop.stringpropertynames();

for(string name:names)

list方法能將屬性列表輸出列印流:printwriter和printstream

properties prop=new properties();

prop.setproperty("zhangsan", "21");

prop.setproperty("lisi", "23");

prop.setproperty("wangwu", "11");

prop.setproperty("zhaoliu", "8");

prop.list(system.out);

store方法能將屬性列表輸出到輸出流,以load()方法載入到properties載入到表中的格式

fileoutputstream fos=new fileoutputstream("e:\\abc.txt");

properties prop=new properties();

prop.setproperty("zhangsan", "21");

prop.setproperty("lisi", "23");

prop.setproperty("wangwu", "11");

prop.setproperty("zhaoliu", "8");

prop.store(fos, "name:age");

fos.close();

fileinputstream fis=new fileinputstream("e:\\abc.txt");

properties prop=new properties();

prop.load(fis);

prop.list(system.out);

* 對已有的配置檔案資訊進行修改

* @throws ioexception

*/public static void main(string args) throws ioexception {

//讀取這個檔案

filereader fr=new filereader("e:\\abc.txt");

//建立集合儲存配置資訊

properties prop=new properties();

//將檔案讀取到這個集合中

prop.load(fr);

//通過集合對這個配置資訊進行修改

prop.setproperty("wangwu", "12");

// prop.list(system.out);

//輸出配置資訊

filewriter fw=new filewriter("e:\\abc.txt");

prop.store(fw, "newinfo");

fw.close();

Properties屬性集合

properties屬性集合概述 是乙個雙列集合,實現了map介面,繼承hashtable類。properties集合的特點 建立物件時不需要指定泛型 鍵和值都是字串 可以和流技術相加使用 可以直接通過流將集合中的資料儲存到檔案中 可以直接從檔案中讀取資料到集合 屬性檔案要求 命名要求 x.prop...

屬性集合類Properties

hashtable implements map public class properties extends hashtable object,object properties 是map的子類 有map的方法 方法 put key,value set keyset prop.put 0001 ...

Properties集合的使用

properties集合是唯一乙個可以和io流相結合的集合 可以將集合中的資料持久化儲存,也可以將硬碟上的資料載入到該集合中。1 private static void show01 14 1 store 持久化資料 2private static void show02 throws ioexce...