Java中有個比較重要的類Properties(Java.util.Properties),主要用于讀取Java的配置文件,各種語言都有自己所支 持的配置文件,配置文件中很多變量是經常改變的,這樣做也是為了方便用戶,讓用戶能夠脫離程序本身去修改相關的變量設置。像Python支持的配置文件 是.ini文件,同樣,它也有自己讀取配置文件的類ConfigParse,方便程序員或用戶通過該類的方法來修改.ini配置文件。在Java中,其配 置文件常為.properties文件,格式為文本文件,文件的內容的格式是“鍵=值”的格式,文本注釋信息可以用"#"來注釋。
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讀取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的系統屬性
1 import java.util.Properties;2 3 public class ReadJVM {4     public static void main(String[] args) {5         Properties pps = System.getProperties();6         pps.list(System.out);7     }8 }結果:

2、隨便新建一個配置文件(Test.properties)
name=JJWeight=4444Height=3333
 1 public class getProperties { 2     public static void main(String[] args) throws FileNotFoundException, IOException { 3         Properties pps = new Properties(); 4         pps.load(new FileInputStream("Test.properties")); 5         Enumeration enum1 = pps.propertyNames();//得到配置文件的名字 6         while(enum1.hasMoreElements()) { 7             String strKey = (String) enum1.nextElement(); 8             String strValue = pps.getProperty(strKey); 9             System.out.println(strKey + "=" + strValue);10         }11     }12 }
3、一個比較綜合的實例
根據key讀取value
讀取properties的全部信息
寫入新的properties信息
 1 //關于Properties類常用的操作 2 public class TestProperties { 3     //根據Key讀取Value 4     public static String GetValueByKey(String filePath, String key) { 5         Properties pps = new Properties(); 6         try { 7             InputStream in = new BufferedInputStream (new FileInputStream(filePath));   8             pps.load(in); 9             String value = pps.getProperty(key);10             System.out.println(key + " = " + value);11             return value;12             13         }catch (IOException e) {14             e.printStackTrace();15             return null;16         }17     }18     19     //讀取Properties的全部信息20     public static void GetAllProperties(String filePath) throws IOException {21         Properties pps = new Properties();22         InputStream in = new BufferedInputStream(new FileInputStream(filePath));23         pps.load(in);24         Enumeration en = pps.propertyNames(); //得到配置文件的名字25         26         while(en.hasMoreElements()) {27             String strKey = (String) en.nextElement();28             String strValue = pps.getProperty(strKey);29             System.out.println(strKey + "=" + strValue);30         }31         32     }33     34     //寫入Properties信息35     public static void WriteProperties (String filePath, String pKey, String pValue) throws IOException {36         Properties pps = new Properties();37         38         InputStream in = new FileInputStream(filePath);39         //從輸入流中讀取屬性列表(鍵和元素對) 40         pps.load(in);41         //調用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。  42         //強制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調用 put 的結果。43         OutputStream out = new FileOutputStream(filePath);44         pps.setProperty(pKey, pValue);45         //以適合使用 load 方法加載到 Properties 表中的格式,  46         //將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流  47         pps.store(out, "Update " + pKey + " name");48     }49     50     public static void main(String [] args) throws IOException{51         //String value = GetValueByKey("Test.properties", "name");52         //System.out.println(value);53         //GetAllProperties("Test.properties");54         WriteProperties("Test.properties","long", "212");55     }56 }結果:
Test.properties中文件的數據為:
#Update long name#Sun Feb 23 18:17:16 CST 2014name=JJWeight=4444long=212Height=3333
新聞熱點
疑難解答