java.util.Properties類

來源:互聯網
上載者:User

標籤:img   chm   tox   沒有   使用   test   att   border   屬性   

學習目標: 1、認識properties檔案,理解其含義,會正確建立properties檔案。2、會使用java.util.Properties類來操作properties檔案。3、掌握相對路徑,能正確書寫一個properties檔案的相對路徑。  一、認識properties檔案 1、properties檔案是一個文字檔2、properties檔案的文法有兩種,一種是注釋,一種屬性配置。 注    釋:前面加上#號 屬性配置:以“鍵=值”的方式書寫一個屬性的配置資訊。3、properties檔案的一個屬性配置資訊值可以換行,但鍵不可以換行。值換行用“\”表示。4、properties的屬性配置索引值前後的空格在解析時候會被忽略。5、properties檔案可以只有鍵而沒有值。也可以僅有鍵和等號而沒有值,但無論如何一個屬性配置不能沒有鍵。  例如,下面一個properties檔案:#正確的properties設定檔 
aaa=1\ 
    11 

bb    =     222 #格式良好的properties檔案 
aaa=111 
bbb=222  二、解讀java.util.Properties類 1、Properties類的階層java.lang.Object  java.util.Dictionary<K,V>      java.util.Hashtable<Object,Object>          java.util.Properties 從層次機構看,Properties類實現了Map介面,因為HashTable實現了Map介面,因此Properties類本質上是一種簡單的Map容器。實際上,Properties類本身表示了對一種Map結構的操作。properties檔案本身就表示了一個“索引值對”的集合。因此,Properties類屬於集合容器的家族,在使用前應該建立一個Properties的容器,實際上就是建立一個預設不帶參數的Properties對象。以後通過別的方式給裡面添加“索引值對”。 2、properties檔案與Properties類的關係通過properties檔案可以填充Properties類。也可以通過xml檔案來填充Properties類。可以通過絕對路徑方式載入Properties檔案資訊,也可以使用相對路徑載入。  三、實踐 1、以絕對相對路徑方式載入properties檔案資訊。2、將Properties對象持久化到一個properties檔案或者一個xml檔案中。3、修改並持久化properties檔案。 測試代碼: 測試的properties檔案:#格式良好的properties檔案 
aaa=111 
bbb=222 測試類別:package stu; 

import java.io.*; 
import java.util.Properties; 

/** 
* Properties類測試 
* User: xiaohui 
* Date: 2008-11-4 21:04:54 
*/ 
public class TestProperties { 
        public static void main(String args[]) throws IOException { 
                testProperties(); 
                test1(); 
        } 

        public static void testProperties() throws IOException { 
                System.out.println("------------testProperties-------------"); 
                //將properties檔案載入到輸入位元組流中 
                InputStream is = new FileInputStream("D:\\myprojects\\lession4\\src\\stu\\ttt.properties"); 
                //建立一個Properties容器 
                Properties prop = new Properties(); 
                //從流中載入properties檔案資訊 
                prop.load(is); 
                //迴圈輸出配置資訊 
                for (Object key : prop.keySet()) { 
                        System.out.println(key + "=" + prop.get(key)); 
                } 

                //定義一個輸出資料流 
                OutputStream os1 = new FileOutputStream("C:\\ttt.xml"); 
                OutputStream os2 = new FileOutputStream("C:\\ttt.properties"); 

                //從Properties對象匯出匯出到xml 
                prop.storeToXML(os1, "我從properties匯出的XML設定檔"); 
                //從Properties對象匯出properties檔案 
                prop.store(os2, "我從properties匯出的XML設定檔"); 

                is.close(); 
                os1.close(); 
                os2.close(); 

                //從xml載入配置資訊,填充Properties容器 
                prop.loadFromXML(new FileInputStream("C:\\ttt.xml")); 
                //迴圈輸出配置資訊 
                System.out.println("我從匯出的xml載入設定檔資訊!"); 
                for (Object key : prop.keySet()) { 
                        System.out.println(key + "=" + prop.get(key)); 
                } 

                //修改Properties對象,並持久化到一個檔案 
                prop.put("呵呵呵", "嘎嘎嘎"); 
                OutputStream os3 = new FileOutputStream("C:\\ttt1.xml"); 
                prop.storeToXML(os3, "我從properties匯出的XML設定檔"); 
                os3.close(); 
        } 

        /** 
         * 以相對路徑方式載入properties檔案 
         * 
         * @throws IOException 
         */ 
        public static void test1() throws IOException { 
                System.out.println("------------test1-------------"); 
                Properties p = new Properties(); 
                p.load(TestProperties.class.getResourceAsStream("/stu/ttt.properties")); 
                for (Object key : p.keySet()) { 
                        System.out.println(key + "=" + p.get(key)); 
                } 
        } 
}  運行結果:------------testProperties------------- 
bbb=222 
aaa=111 
我從匯出的xml載入設定檔資訊! 
bbb=222 
aaa=111 
------------test1------------- 
bbb=222 
aaa=111 

Process finished with exit code 0 C:盤下寫入的檔案如下:    呵呵,全部達到預期目標。

java.util.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.