標籤:
一、琅序
java中的properties檔案是一種設定檔,主要用於表達配置資訊,檔案類型為*.properties,格式為文字檔,檔案的內容是格式是"鍵=值"的格式,在properties檔案中,可以用"#"來作注釋,properties檔案在Java編程中用到的地方很多,操作很方便。
------------------------------------------------------
Java.util 中,Properties類繼承自 Hashtable
1. getProperty ( String key) , 用指定的鍵在此屬性列表中搜尋屬性。也就是通過參數 key ,得到 key 所對應的 value。
2. load ( InputStream inStream) ,從輸入資料流中讀取屬性列表(鍵和元素對)。通過對指定的檔案(比如說上面的 test.properties 檔案)進行裝載來擷取該檔案中的所有鍵 - 值對,以供 getProperty ( String key) 搜尋。
3. setProperty ( String key, String value) ,調用 Hashtable 的方法 put方法。它通過調用基類的put方法來設定鍵 - 值對。
4. store ( OutputStream out, String comments) ,以適合使用 load 方法載入到 Properties 表中的屬性列表(鍵和元素對)寫入輸出資料流。與 load 方法相反,該方法將鍵 - 值對寫入到指定的檔案中去。
5. clear () ,清除所有裝載的鍵 - 值對。該方法在基類中提供。
------------------------------------------------------
二、操作properties檔案的java方法
package com.hibernate.utils.util1;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Properties;import java.util.Set;public final class PropertiesUtil { /** * 讀取某個屬性的值 */ public static String getProperty(String fileName, String propertyName) { Properties prop = new Properties(); InputStream in = null; try { in = new FileInputStream(getFilePath() + fileName); prop.load(in); String propertyValue = prop.getProperty(propertyName); return propertyValue; } catch (IOException e) { e.printStackTrace(); return null; } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 增加或修改屬性 */ public static void saveOrUpdateProperty(String fileName, String propertyName, String propertyValue) { Map<String, String> toSaveMap = getProperties(fileName); toSaveMap.put(propertyName, propertyValue);// 添加或更新屬性 Properties prop = new Properties(); OutputStream out = null; try { out = new FileOutputStream(getFilePath() + fileName); } catch (FileNotFoundException e) { e.printStackTrace(); } prop.putAll(toSaveMap); try { prop.store(out, "come from add Or update action");// 更新properties檔案 } catch (IOException e) { e.printStackTrace(); } } /** * 刪除屬性 */ public static void deleteProperty(String fileName, String propertyName) { Map<String, String> toSaveMap = getProperties(fileName); toSaveMap.remove(propertyName);// 移除屬性 Properties prop = new Properties(); OutputStream out = null; try { out = new FileOutputStream(getFilePath() + fileName); } catch (FileNotFoundException e) { e.printStackTrace(); } prop.putAll(toSaveMap); try { prop.store(out, "");// 更新properties檔案 } catch (IOException e) { e.printStackTrace(); } } /** * 擷取類所在的絕對路徑 */ public static String getFilePath() { return PropertiesUtil.class.getClass().getResource("/").getPath(); } /** * 讀取properties檔案的所有屬性 */ public static Map<String, String> getProperties(String fileName) { Properties prop = new Properties(); try { InputStream in = new FileInputStream(getFilePath() + fileName); prop.load(in);// 載入屬性值 } catch (IOException e) { e.printStackTrace(); } Map<String, String> toSaveMap = new HashMap<String, String>(); Set<Object> keys = prop.keySet(); Iterator<Object> it = keys.iterator(); while (it.hasNext()) { String key = (String) it.next(); String value = (String) prop.get(key); toSaveMap.put(key, value); } return toSaveMap; }}
----------------------------------------致青春----------------------------------------
Java操作properties檔案