Java學習-019-Properties 檔案讀取執行個體原始碼

來源:互聯網
上載者:User

標籤:

在這幾天的學習過程中,有開發的朋友告知我,每個程式設計語言基本都有相應的設定檔支援類,像 Python 程式設計語言中支援的 ini 檔案及其對應的設定檔讀取類 ConfigParse,通過這個類,使用者可以方便的修改 ini 設定檔。在 Java 中對應的設定檔為 properties 格式的文字檔,其對應的內容格式為 “鍵=值” ,文本注釋資訊可以用 “#” 注釋。同時 Java 語言中對應的類為 Properties(java.util.Properties),即為讀取 properties 檔案的類,繼承於 Hashtable (如下所示),使用者使用這個類可以很方便的操作 properties 檔案。

下面首先看一下其對應的結果主要方法,如下所示:

1、getProperty(String key),用指定的鍵在此屬性列表中搜尋屬性。即通過參數 key 得到所對應的 value。
2、load(InputStream inStream),從輸入資料流中讀取屬性列表(鍵和元素對)。通過對指定的檔案進行裝載來擷取該檔案中的所有索引值對。
3、setProperty(String key, String value),調用 Hashtable 的方法 put 。他通過調用基類的put方法來設定鍵-值對。
4、store(OutputStream out, String comments),以適合使用 load 方法載入到 Properties 表中的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出資料流。與 load 方法相反,該方法將鍵 - 值對寫入到指定的檔案中去。
5、clear(),清除所有裝載的 鍵 - 值對。該方法在基類中提供。

下面通過針對 Properties 設定檔的讀取進行執行個體代碼示範,以加深對 Properties 類的理解。

小二上碼。。。

以下為原始的 properties 檔案內容:

 1 # oracle config 2 db_type=oracle 3 driver=oracle.jdbc.driver.OracleDriver 4 host=127.0.0.1 5 port=1521 6 database=test 7 user=aaron 8 pass=ffp 9 sql=select * from hr t where t.no = ‘1521‘10 desc=oracle\u6570\u636E\u5E93\u8FDE\u63A5\u914D\u7F6E
PropertiesRead.properties 檔案內容

properties 設定檔方法的源碼(返回 Properties):

 1     /** 2      * @function 檔案讀取: properties 檔案 3      * @description PS:注意讀取中文時的亂碼處理 4      *  5      * @author Aaron.ffp 6      * @version V1.0.0: autoUISelenium main.java.aaron.java.tools FileUtils.java propertiesRead, 2014-11-20 16:29:09 Exp $ 7      *  8      * @param filename 檔案路徑 9      * @return Properties10      */11     public Properties propertiesRead(String filename){12         Properties properties = new Properties();13         14         /* 參數校正: 為null或Null 字元串時, 拋出參數非法異常 */15         if (filename == null || "".equals(filename) || !assertFileType(filename, "PROPERTIES")) {16             throw new IllegalArgumentException();17         }18         19         try {20             /* 擷取檔案資料流 */21             InputStream is = new FileInputStream(filename);22             /* 負載檔案資料流 */23             properties.load(is);24             /* 關閉檔案資料流 */25             is.close();26         } catch (IOException ioe) {27             this.message = "檔案 {" + filename + "} 讀取失敗!";28             this.logger.error(this.message, ioe);29         }30         31         return properties;32     }
Java 類 Properties 讀取 properties 設定檔方法源碼

測試方法源碼:

 1     /** 2      * Test : properties read for txt file 3      *  4      * @author Aaron.ffp 5      * @version V1.0.0: autoUISelenium test.java.aaron.java.tools FileUtilsTest.java test_propertiesRead, 2014-11-20 16:35:09 Exp $ 6      * 7      */ 8     public void test_propertiesRead(){ 9         this.message = "\n\n\nTEST:FileUtils.propertiesRead(String filename)";10         this.logger.debug(this.message);11         12         this.fu = new FileUtils();13         String filename = this.constantslist.PROJECTHOME + this.constantslist.FILESEPARATOR + 14                           "testng-temp" + this.constantslist.FILESEPARATOR + "propertiesRead.properties";15         16         Properties prop = this.fu.propertiesRead(filename);17         18         // get keys from properties19         Enumeration<?> enu = prop.propertyNames();20         21         // print-122         prop.list(System.out);23         24         System.out.println("\n\n");25         26         // print-227         String key = "";28         while (enu.hasMoreElements()) {29             key = (String)enu.nextElement();30             System.out.println(key + " = " + prop.getProperty(key));31         }32     }
Java 類 Properties 讀取 properties 配置類檔案方法測試源碼

程式運行結果如下所示:

PS:若需使用此文中的源碼,需要將 properties 檔案中的內容儲存至本地,並將測試方法源碼中的檔案路徑改為本地路徑,同時對上述源碼進行適當修改才可成功運行,請知悉!

 

至此, Java學習-019-Properties 檔案讀取執行個體原始碼 順利完結,希望此文能夠給初學 Java 的您一份參考。

最後,非常感謝親的駐足,希望此文能對親有所協助。熱烈歡迎親一起探討,共同進步。非常感謝! ^_^

Java學習-019-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.