properties檔案的訪問

2021-08-15 05:26:03 字數 818 閱讀 5718

properties文字檔案,訪問鍵值對,處理資料比較方便

//獲取properties文字值

properties prop = new properties();    //乙個類搞定

inputstream fis = new fileinputstream(filepath);

prop.load(fis);

set keys = prop.keyset();                   //所有的鍵

terator iterator =keys.iterator();

while(iterator.hashnext()){

string key =(string) iterator.next();     //遍歷得到key

//往properties文字存值

properties prop  = new properties();

prop.clear();

//清空屬性表裡面的值

prop.setproperty(key, "comcond");   //comcode,properties注釋

outputstream fw = new fileoutputstream(filepath);

prop.store(fw, "comcond");   //通過輸出流往prop檔案存值

這裡說明一下filepath,專案裡採用相對路徑(可理解為url),例:

src/resources/config/auditconfigcomcond.properties

絕對路徑(物理路徑),存在磁碟之類的路徑:

c:\bea

載入properties檔案

1 使用絕對路徑 string filename c a.properties properties p new properties inputstream input new fileinputstream filename p.load input 缺點 使用了絕對路徑,不利於專案的拷貝 頻繁...

properties 檔案讀取

假設在src根目錄下有檔案 config.properties 下面 就是讀取這個檔案的例子程式 方法1 properties prop new properties inputstream in object.class.getresourceasstream config.properties ...

Properties讀寫檔案

properties是hashtable 執行緒安全 不能以null作為key或者value properties 類表示了乙個持久的屬性集。properties 可儲存在流中或從流中載入。屬性列表中每個鍵及其對應值都是乙個字串。所儲存的資料都是字串類 properties中每個鍵及其對應處理的值都...