Java的Properties類

來源:互聯網
上載者:User

標籤:hit   tor   tox   lang   錯誤   sum   i/o   path   面向   

Java在util包下提供了一個Properties類來操作Properties資源檔:

public class Properties extends Hashtable<Object, Object>

它繼承了Hashtable<Object, Object> 是 Dictionary<K,V>的間接子類。

同時,它實現了Serializable, Cloneable, Map<Object, Object> 介面。

直接的子類是Provider

 

Properties類表示屬性的持久化,在程式中的屬性通常是暫時存在的。如果我們需要讓屬性長久存在,那麼就需要把屬性存入資料庫或者檔案當中,Properties類用來將屬性存入properties檔案當中,以持久化。

Properties可儲存在流中或者從流當中載入,屬性列表中每一個索引值都是字串。

它實現了Map介面,當然也可以使用put等方法,但是不建議使用put,因為該方法允許其值是非字元形,所以為了值的安全,我們建議使用setProperty方法,這樣在編譯期就可以檢查值是否正確。

如果是xml檔案格式來儲存properties屬性,那麼需要聲明

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

例如:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"><properties><entry key="age">40</entry><entry key="name">lay</entry></properties>

API詳見:

方法摘要
 String getProperty(String key) 
          用指定的鍵在此屬性列表中搜尋屬性。
 String getProperty(String key, String defaultValue) 
          用指定的鍵在屬性列表中搜尋屬性。
 void list(PrintStream out) 
          將屬性列表輸出到指定的輸出資料流。
 void list(PrintWriter out) 
          將屬性列表輸出到指定的輸出資料流。
 void load(InputStream inStream) 
          從輸入資料流中讀取屬性列表(鍵和元素對)。
 void load(Reader reader) 
          按簡單的面向行的格式從輸入字元流中讀取屬性列表(鍵和元素對)。
 void loadFromXML(InputStream in) 
          將指定輸入資料流中由 XML 文檔所表示的所有屬性載入到此屬性工作表中。
 Enumeration<?> propertyNames() 
          返回屬性列表中所有鍵的枚舉,如果在主屬性列表中未找到同名的鍵,則包括預設屬性列表中不同的鍵。
 void save(OutputStream out, String comments) 
          已淘汰。 如果在儲存屬性列表時發生 I/O 錯誤,則此方法不拋出 IOException。儲存屬性列表的首選方法是通過 store(OutputStream out, String comments) 方法或 storeToXML(OutputStream os, String comment) 方法來進行。
 Object setProperty(String key, String value) 
          調用 Hashtable 的方法 put。
 void store(OutputStream out, String comments) 
          以適合使用 load(InputStream) 方法載入到 Properties 表中的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出資料流。
 void store(Writer writer, String comments) 
          以適合使用 load(Reader) 方法的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出字元。
 void storeToXML(OutputStream os, String comment) 
          發出一個表示此表中包含的所有屬性的 XML 文檔。
 void storeToXML(OutputStream os, String comment, String encoding) 
          使用指定的編碼發出一個表示此表中包含的所有屬性的 XML 文檔。
 Set<String> stringPropertyNames() 
          返回此屬性列表中的鍵集,其中該鍵及其對應值是字串,如果在主屬性列表中未找到同名的鍵,則還包括預設屬性列表中不同的鍵。
// 個別方法使用範例
public
class test { public static void main(String[] args) { InputStream inputStream = null; // 從xml中讀取 inputStream = test.class.getResourceAsStream("/fileXml.xml"); Properties user = new Properties(); try { user.loadFromXML(inputStream); user.setProperty("age", "40"); System.out.println(user.getProperty("name")); System.out.println(user.getProperty("age")); // 擷取項目路徑 String path = test.class.getResource("/").getPath(); System.out.println(path); // 輸出為xml, 預設為 utf-8 user.storeToXML(new FileOutputStream(path + "/file.xml"), "評論內容"); user.list(System.out); } catch (IOException e) { e.printStackTrace(); } }}

 

Java的Properties類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.