標籤:
java中有個比較重要的類:Properties類,該類主要用於讀取java的設定檔。每種語言都自己所支援的設定檔。java中的配置常為*.perperties檔案,檔案格式為文字格式設定,內容格式為“健=值”對格式,文本注釋資訊可用#注釋。
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 表中的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出資料流。與 load 方法相反,該方法將鍵 - 值對寫入到指定的檔案中去。
5. clear (),清除所有裝載的 鍵 - 值對。該方法在基類中提供。
二、Java讀取Properties檔案
Java讀取Properties檔案的方法有很多
但是最常用的還是通過java.lang.Class類的getResourceAsStream(String name)方法來實現,如下可以這樣調用:
InputStream in = getClass().getResourceAsStream("資源Name");作為我們寫程式的,用此一種足夠。
或者下面這種也常用:
InputStream in = new BufferedInputStream(new FileInputStream(filepath));
三、相關執行個體
下面列舉幾個執行個體,加深對Properties類的理解和記憶。
我們知道,Java虛擬機器(JVM)有自己的系統設定檔(system.properties),我們可以通過下面的方式來擷取。
1、擷取JVM的系統屬性
public class Property { public static void main(String args[]){ System.out.println(new Date()); Properties p=System.getProperties(); p.list(System.out); //print all properties about JVM }}
2.讀取本地設定檔test.properties並輸出
public class Property { public static void main(String args[]){// System.out.println(new Date());// Properties p=System.getProperties();// p.list(System.out); //print all properties about JVM Properties text=new Properties(); try { text.load(new FileInputStream("E:\\workspace\\ch01\\text.properties")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } @SuppressWarnings("rawtypes") /* Enumeration介面也在java.util包中 它的功能與Iterator差不多 都是用來遍曆集合中的元素 但是枚舉Enumeration只提供了遍曆Vector和Hashtable類型集合元素的功能*/ Enumeration num=text.propertyNames(); //擷取配件檔案內容中的關於key的Enumeration while(num.hasMoreElements()){ String key=(String)num.nextElement(); String value=text.getProperty(key); System.out.println(key + "=" + value); } }}
結果:
age=23name=linyuhuanweight=140long=212
3、一個比較綜合的執行個體
根據key讀取value
讀取properties的全部資訊
寫入新的properties資訊
package ch01;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Enumeration;import java.util.Properties;/** * 關於Properties類的常用操作 * @author lin * 2015年6月30日 */public class TestProperties { public static void main(String[] args) throws IOException{ WriteProperties("E:\\workspace\\ch01\\text.properties", "add2","2121"); } //根據key讀取value public static String GetValueByKey(String filePath,String key){ Properties pps = new Properties(); try{ InputStream in= new BufferedInputStream(new FileInputStream(filePath)); pps.load(in); String value = pps.getProperty(key); System.out.println(key + "=" + value); return value; }catch(IOException e){ e.printStackTrace(); return null; } } //讀取設定檔的全部資訊 @SuppressWarnings("rawtypes") public static void GetAllProperties(String filePath) throws IOException{ Properties pps = new Properties(); InputStream in = new BufferedInputStream(new FileInputStream(filePath)); pps.load(in); Enumeration num = pps.propertyNames(); //擷取設定檔中的所有屬性名稱enumeration while(num.hasMoreElements()){ String key= (String)num.nextElement(); String value=pps.getProperty(key); System.out.println(key + "=" + value); } } //寫入Properties資訊 public static void WriteProperties(String filePath,String pKey,String pValue) throws IOException { Properties pps = new Properties(); InputStream in= new FileInputStream(filePath); pps.load(in); OutputStream out = new FileOutputStream(filePath); pps.setProperty(pKey, pValue); pps.store(out, "Update " + pKey + " name"); }}
結果:test.properties檔案
#Update add2 name#Tue Jun 30 17:07:55 CST 2015age=23name=linyuhuanweight=140add2=2121long=212
參考連結:http://www.cnblogs.com/bakari/p/3562244.html
java中的Properties類的操作