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.49028c.com/bakari/p/3562244.html
新聞熱點
疑難解答