Java知識積累——Properties類的使用樣本

來源:互聯網
上載者:User

最近一直在研究properties設定檔,突然碰到了一個java的類,名為Properties。該類繼承自HashTable,提供的方法很像Map的實作類別HashMap。一時間激發了我對此類的關注和研究,通過找資料和自行調試,發現該類能夠在程式運行初期給我們提供協助。通過解析前置檔案(含程式需要的某些參數),獲得程式運行所需的配置資訊,存入Properties類中,供程式調用。

Properties類的作用就像其名字所體現的一樣,主要是解決屬性問題的,說白了就是此類和設定檔的關係十分曖昧。現將Properties類的常見使用情境概括如下:

1.從properties設定檔中讀取資料,解析key-value對,存入Properties類中。

2.從xml配置問價那種讀取資料,解析key-value對,存入Properties類中。

3.將Properties類中儲存的key-value對,輸出到properties設定檔中。

4.將Properties類中儲存的key-value對,輸出到xml設定檔中。

見面四個函數分別對應了這四種情況:

 1 //讀取properties檔案的配置資訊到Properties類中 2 public void readPropFile(){ 3     Properties prop = new Properties(); 4     Enumeration<String> en = null; 5     String key = null; 6      7     InputStream input = null; 8     try { 9         input = this.getClass().getClassLoader().getResourceAsStream("test.properties");10         prop.load(input);11         en = (Enumeration<String>) prop.propertyNames();12         //prop.list(System.out);13         while(en.hasMoreElements()){14             key = en.nextElement();15             System.out.println(key+"---"+prop.getProperty(key));16         }17     } catch (Exception e) {18         e.printStackTrace();19     } finally{20         try {21             input.close();22         } catch (Exception e2) {23             e2.printStackTrace();24         }25     }26 }27 28 //讀取xml檔案的配置資訊到Properties類中29 public void readXmlFile(){30     Properties prop = new Properties();31     Enumeration<String> en = null;32     String key = null;33     34     InputStream input = null;35     try {36         input = this.getClass().getClassLoader().getResourceAsStream("test.xml");37         prop.loadFromXML(input);38         en = (Enumeration<String>) prop.propertyNames();39         //prop.list(System.out);40         while(en.hasMoreElements()){41             key = en.nextElement();42             System.out.println(key+"---"+prop.getProperty(key));43         }44     } catch (Exception e) {45         e.printStackTrace();46     } finally{47         try {48             input.close();49         } catch (Exception e2) {50             e2.printStackTrace();51         }52     }53 }54 55 //將Properties類中的key-value對,存到properties檔案中56 public void printProp(){57     Properties prop = new Properties();58     prop.setProperty("content1", "there is no problem");59     prop.setProperty("content2", "hello world");60     61     //PrintWriter outputFile = null;62     FileOutputStream outputFile = null;63     try {64         //outputFile = new PrintWriter(new File("src/target.properties"));65         outputFile = new FileOutputStream("src/target.properties");66         prop.store(outputFile, "test target file");67     } catch (Exception e) {68         e.printStackTrace();69     } finally{70         try {71             outputFile.close();72         } catch (Exception e2) {73             e2.printStackTrace();74         }75     }76 }77 78 //將Properties類中的key-value對,存到xml檔案中79 public void printXML(){80     Properties prop = new Properties();81     prop.setProperty("content1", "there is no problem");82     prop.setProperty("content2", "hello world");83     84     //PrintWriter outputFile = null;85     FileOutputStream outputFile = null;86     try {87         //outputFile = new PrintWriter(new File("src/target.properties"));88         outputFile = new FileOutputStream("src/target.xml");89         prop.storeToXML(outputFile, "test target xml file");90     } catch (Exception e) {91         e.printStackTrace();92     } finally{93         try {94             outputFile.close();95         } catch (Exception e2) {96             e2.printStackTrace();97         }98     }99 }

輸出到檔案可使用Properties類的list方法,也可使用store方法。

輸出後的內容如下

properties檔案

#test target file#Thu Mar 14 15:59:01 CST 2013content2=hello worldcontent1=there is no problem

xml檔案

1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>2 <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">3 <properties>4 <comment>test target xml file</comment>5 <entry key="content2">hello world</entry>6 <entry key="content1">there is no problem</entry>7 </properties>

需要強調的是,對於xml檔案的讀取,僅支援如上代碼所示的格式,此格式為properties.dtd中規定的。其他規則的xml配置將不能被正確識別且會拋出異常。而對於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.