Java學習-023-Properties 類 XML 設定檔讀取及寫入原始碼

來源:互聯網
上載者:User

標籤:

之前的幾篇 Properties 文章已經講述過了 Java 設定檔類 Properties 的基本用法,查看 JDK 的協助文檔時,也可看到在 Properties 類中還有兩個方法 loadFromXML(InputStream) 和 storeToXml(OutputStream, String, String),由方法名中的 xml 不難確定這兩個方法分別是讀取/寫入資料到 xml 檔案。JDK 文檔部分如下所示:

因而此文將通過源碼執行個體示範 Properties 類是如何將資料寫入 xml 檔案中,及如何讀取資料的。當然,寫入 xml 的資料需要符合一定的標準,即以 索引值對 的形式存在;同時,產生的 xml 檔案的格式可參考中的 properties.dtd。

 

話不多言,小二上碼。。。敬請各位小主參閱。若有不足之處,敬請大神指正,不勝感激!

建立 xml 檔案源碼如下所示:

 1     /** 2      * @function Write data to xml file by Properties 3      *  4      * @author Aaron.ffp 5      * @version V1.0.0: autoUISelenium main.java.aaron.java.tools FileUtils.java propertiesWriteXml, 2014-11-20 16:47:47 Exp $ 6      *  7      * @param filename : full path 8      * @param author   : creator 9      * @param configs  : file contents10      * @return boolean11      */12     public boolean propertiesWriteXml(String filename, String author, ArrayList<String[]> configs){13         Properties properties = new Properties();14         boolean success = false;15         16         /* 參數校正: 參數非法時, 拋出參數非法異常 */17         if (filename == null || "".equals(filename) || author == null || "".equals(author) || configs.isEmpty()) {18             throw new IllegalArgumentException();19         }20         21         try {22             OutputStream os = new FileOutputStream(filename);23             24             for (String[] row : configs) {25                 properties.setProperty(row[0].trim(), row[1].trim());26             }27             28             properties.storeToXML(os, "author:" + author, "UTF-8");29             os.close();30             31             success = true;32         } catch (IOException ioe) {33             this.message = "檔案 {" + filename + "} 寫入失敗!";34             this.logger.error(this.message, ioe);35         }36         37         return success;38     }
Properties 寫入 Java xml 設定檔源碼

讀取 xml 檔案源碼如下所示:

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

測試源碼如下所示:

 1     /** 2      * Test : Write/Read data to/from XML file 3      *  4      * @author Aaron.ffp 5      * @version V1.0.0: autoUISelenium test.java.aaron.java.tools FileUtilsTest.java test_propertiesWriteXml, 2014-11-20 16:53:53 Exp $ 6      * 7      */ 8     public void test_propertiesWriteReadXml(){ 9         this.message = "\n\n\nTEST:FileUtils.propertiesWriteXml(String filename, String author, ArrayList<String[]> configs)" + 10                            "\n      FileUtils.propertiesReadXml(String filename)";11         this.logger.debug(this.message);12         13         this.fu = new FileUtils();14         String filename = this.constantslist.PROJECTHOME + this.constantslist.FILESEPARATOR + 15                           "testng-temp" + this.constantslist.FILESEPARATOR + "test_propertiesWriteReadXml.xml";16         17         ArrayList<String[]> configs = new ArrayList<String[]>();18         19         String author = "Aaron.ffp";20         21         for (int i = 0; i < 10; i++) {22             String[] row = new String[2];23             24             row[0] = i + "";25             row[1] = "The row number is " + (i + 1);26             27             configs.add(i, row);28         }29         30         if (this.fu.propertiesWriteXml(filename, author, configs)) {31             Assert.assertEquals(this.fu.propertiesGetValue(this.fu.propertiesReadXml(filename), "2"), "The row number is 3", "Test case failed.");32         } else {33             this.message = "Test case failed, please check the data file : " + filename; 34             Assert.fail(this.message);35         };36     }
Properties 讀取 Java xml 設定檔測試源碼

程式執行結果如下所示:

產生的 xml 檔案內容如下所示:

至此, Java學習-023-Properties 類建立 XML 檔案 順利完結,希望此文能夠給初學 Java 的您一份參考。

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

Java學習-023-Properties 類 XML 設定檔讀取及寫入原始碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.