• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            為生存而奔跑

               :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
              271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks

            留言簿(5)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            積分與排名

            • 積分 - 330192
            • 排名 - 74

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            轉(zhuǎn)自http://virgos.javaeye.com/blog/326014
            Properties 類已不是新東西了,它在 Java 編程的早期就有了,并且?guī)缀鯖](méi)有什么變化。J2SE 的 Tiger 版本增強(qiáng)了這個(gè)類,不僅可以用它在單獨(dú)一行中指定用等號(hào)分隔的多個(gè)鍵-值對(duì),還可以用XML 文件裝載和保存這些鍵-值對(duì)。在 馴服 Tiger的這一期文章中,John Zukowski 展示了如何駕馭這匹新一代的“役馬”。

                J2SE 1.5 以前的版本要求直接使用 XML 解析器來(lái)裝載配置文件并存儲(chǔ)設(shè)置。雖然這并非是一件困難的事情,并且解析器是平臺(tái)的標(biāo)準(zhǔn)部分,但是額外的工作總是有點(diǎn)讓人煩。最近更新的 java.util.Properties 類現(xiàn)在提供了一種為程序裝載和存儲(chǔ)設(shè)置的更容易的方法: loadFromXML(InputStream is) 和 storeToXML(OutputStream os, String comment) 方法。

            Properties 基本知識(shí)
            如果不熟悉 java.util.Properties 類,那么現(xiàn)在告訴您它是用來(lái)在一個(gè)文件中存儲(chǔ)鍵-值對(duì)的,其中鍵和值是用等號(hào)分隔的,如清單 1 所示。

            清單 1. 一組屬性示例
            foo=bar
            fu=baz

            將清單 1 裝載到 Properties 對(duì)象中后,您就可以找到兩個(gè)鍵( foo 和 fu )和兩個(gè)值( foo 的 bar 和 fu 的 baz )了。這個(gè)類支持帶 \u 的嵌入 Unicode 字符串,但是這里重要的是每一項(xiàng)內(nèi)容都當(dāng)作 String 。

            清單 2 顯示了如何裝載屬性文件并列出它當(dāng)前的一組鍵和值。只需傳遞這個(gè)文件的 InputStream 給 load() 方法,就會(huì)將每一個(gè)鍵-值對(duì)添加到 Properties 實(shí)例中。然后用 list() 列出所有屬性或者用 getProperty() 獲取單獨(dú)的屬性。
            清單 2. 裝載屬性
            Java代碼
            1. import java.util.*;  
            2. import java.io.*;  
            3.   
            4. public class LoadSample {  
            5.     public static void main(String args[]) throws Exception {  
            6.       Properties prop = new Properties();  
            7.       FileInputStream fis =   
            8.         new FileInputStream("sample.properties");  
            9.       prop.load(fis);  
            10.       prop.list(System.out);  
            11.       System.out.println("\nThe foo property: " +  
            12.           prop.getProperty("foo"));  
            13.     }  
            14. }  

            運(yùn)行 LoadSample 程序生成如清單 3 所示的輸出。注意 list() 方法的輸出中鍵-值對(duì)的順序與它們?cè)谳斎胛募械捻樞虿灰粯印?Properties 類在一個(gè)散列表(hashtable,事實(shí)上是一個(gè) Hashtable 子類)中儲(chǔ)存一組鍵-值對(duì),所以不能保證順序。

            清單 3. LoadSample 的輸出

            -- listing properties --
            fu=baz
            foo=bar

            The foo property: bar

            XML 屬性文件
            這里沒(méi)有什么新內(nèi)容。 Properties 類總是這樣工作的。不過(guò),新的地方是從一個(gè) XML 文件中裝載一組屬性。它的 DTD 如清單 4 所示。

            清單 4. 屬性 DTD

            dtd 寫道
            Java代碼
            1. <?xml version="1.0" encoding="UTF-8"?>   
            2. <!-- DTD for properties -->   
            3. <!ELEMENT properties ( comment?, entry* ) >   
            4. <!ATTLIST properties version CDATA #FIXED "1.0">   
            5. <!ELEMENT comment (#PCDATA) >   
            6. <!ELEMENT entry (#PCDATA) >   
            7. <!ATTLIST entry key CDATA #REQUIRED>  



            如果不想細(xì)讀 XML DTD,那么可以告訴您它其實(shí)就是說(shuō)在外圍 <properties> 標(biāo)簽中包裝的是一個(gè) <comment> 標(biāo)簽,后面是任意數(shù)量的 <entry> 標(biāo)簽。對(duì)每一個(gè) <entry> 標(biāo)簽,有一個(gè)鍵屬性,輸入的內(nèi)容就是它的值。清單 5 顯示了 清單 1中的屬性文件的 XML 版本是什么樣子的。

            清單 5. XML 版本的屬性文件
            Java代碼
            1. <?xml version="1.0" encoding="UTF-8"?>  
            2. <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">  
            3. <properties>  
            4. <comment>Hi</comment>  
            5. <entry key="foo">bar</entry>  
            6. <entry key="fu">baz</entry>  
            7. </properties>  

            如果清單 6 所示,讀取 XML 版本的 Properties 文件與讀取老格式的文件沒(méi)什么不同。
            清單 6. 讀取 XML Properties 文件
            Java代碼
            1. import java.util.*;  
            2. import java.io.*;  
            3.   
            4. public class LoadSampleXML {  
            5.     public static void main(String args[]) throws Exception {  
            6.       Properties prop = new Properties();  
            7.       FileInputStream fis =  
            8.         new FileInputStream("sampleprops.xml");  
            9.       prop.loadFromXML(fis);  
            10.       prop.list(System.out);  
            11.       System.out.println("\nThe foo property: " +  
            12.           prop.getProperty("foo"));  
            13.     }  
            14. }  

            關(guān)于資源綁定的說(shuō)明
            雖然 java.util.Properties 類現(xiàn)在除了支持鍵-值對(duì),還支持屬性文件作為 XML 文件,不幸的是,沒(méi)有內(nèi)置的選項(xiàng)可以將 ResourceBundle 作為一個(gè) XML 文件處理。是的, PropertyResourceBundle 不使用 Properties 對(duì)象來(lái)裝載綁定,不過(guò)裝載方法的使用是硬編碼到類中的,而不使用較新的 loadFromXML() 方法。

            運(yùn)行清單 6 中的程序產(chǎn)生與原來(lái)的程序相同的輸出,如 清單 2所示。

            保存 XML 屬性
            新的 Properties 還有一個(gè)功能是將屬性存儲(chǔ)到 XML 格式的文件中。雖然 store() 方法仍然會(huì)創(chuàng)建一個(gè)類似 清單 1 所示的文件,但是現(xiàn)在可以用新的 storeToXML() 方法創(chuàng)建如 清單 5 所示的文件。只要傳遞一個(gè) OutputStream 和一個(gè)用于注釋的 String 就可以了。清單 7 展示了新的 storeToXML() 方法。

            清單 7. 將 Properties 存儲(chǔ)為 XML 文件
            Java代碼
            1. import java.util.*;  
            2. import java.io.*;  
            3.   
            4. public class StoreXML {  
            5.     public static void main(String args[]) throws Exception {  
            6.       Properties prop = new Properties();  
            7.       prop.setProperty("one-two""buckle my shoe");  
            8.       prop.setProperty("three-four""shut the door");  
            9.       prop.setProperty("five-six""pick up sticks");  
            10.       prop.setProperty("seven-eight""lay them straight");  
            11.       prop.setProperty("nine-ten""a big, fat hen");  
            12.       FileOutputStream fos =  
            13.         new FileOutputStream("rhyme.xml");  
            14.       prop.storeToXML(fos, "Rhyme");  
            15.       fos.close();  
            16.     }  
            17. }  

            運(yùn)行清單 7 中的程序產(chǎn)生的輸出如清單 8 所示。

            清單 8. 存儲(chǔ)的 XML 文件
            Java代碼
            1. <?xml version="1.0" encoding="UTF-8"?>  
            2. <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">  
            3. <properties>  
            4. <comment>Rhyme</comment>  
            5. <entry key="seven-eight">lay them straight</entry>  
            6. <entry key="five-six">pick up sticks</entry>  
            7. <entry key="nine-ten">a big, fat hen</entry>  
            8. <entry key="three-four">shut the door</entry>  
            9. <entry key="one-two">buckle my shoe</entry>  
            10. </properties>  

            結(jié)束語(yǔ)
            使用 XML 文件還是使用老式的 a=b 類型的文件完全取決于您自己。老式文件從內(nèi)存的角度看肯定是輕量級(jí)的。不過(guò),由于 XML 的普遍使用,人們會(huì)期望 XML 格式流行起來(lái),因?yàn)樗呀?jīng)被廣泛使用了,只不過(guò)沒(méi)有用到 Properties 對(duì)象。選擇完全在您。分析軟件包 private XMLUtils 類的源代碼以獲得關(guān)于所使用的 XML 解析的更多信息。
            Java代碼
            1. import java.io.FileInputStream;  
            2. import java.io.IOException;  
            3. import java.io.InputStream;  
            4. import java.util.Properties;  
            5. /** 
            6. * 實(shí)現(xiàn)properties文件的讀取 
            7. * @author bbflyerwww 
            8. * @date 2006-08-02 
            9. */  
            10. public class PTest {  
            11.       public static void main(String[] args) {  
            12.           try {  
            13.               long start = System.currentTimeMillis();  
            14.               InputStream is = new FileInputStream("conf.properties");  
            15.               Properties p = new Properties();  
            16.               p.load(is);  
            17.               is.close();  
            18.               System.out.println("SIZE : " + p.size());  
            19.               System.out.println("homepage : " + p.getProperty("homepage"));  
            20.               System.out.println("author : " + p.getProperty("author"));  
            21.               System.out.println("school : " + p.getProperty("school"));  
            22.               System.out.println("date : " + p.getProperty("date"));  
            23.               long end = System.currentTimeMillis();   
            24.               System.out.println("Cost : " + (end - start));  
            25.           } catch (IOException ioe) {  
            26.               ioe.printStackTrace();  
            27.           }  
            28.       }  
            29. }  

            conf.properties
            Java代碼
            1. # Configuration fileauthor = bbflyerwww  
            2. school = WuHan University  
            3. date = 2006-08-02  


            Result

            SIZE:4
            author : bbflyerwww
            school : WuHan University
            date : 2006-08-02
            Cost : 0
            posted on 2011-01-23 23:36 baby-fly 閱讀(2442) 評(píng)論(1)  編輯 收藏 引用 所屬分類: Java

            Feedback

            # re: Java properties使用各位指點(diǎn)一下 程序運(yùn)行結(jié)果為找不到指定文件 2011-03-17 16:04 冰原植被
            import java.io.FileInputStream;
            import java.util.Properties;

            public class PropertiesTest {

            public static void main(String[] args) throws Exception {
            // TODO Auto-generated method stub
            FileInputStream propFile=new FileInputStream("myProperties.txt");
            Properties p=new Properties(System.getProperties());
            p.load(propFile);
            System.setProperties(p);
            System.getProperties().list(System.out);

            }

            }  回復(fù)  更多評(píng)論
              

            久久精品嫩草影院| 久久免费美女视频| 久久综合九色欧美综合狠狠| 精品无码久久久久久尤物| 久久综合亚洲欧美成人| 久久久久人妻一区精品性色av| 国内高清久久久久久| 亚洲AV无码久久精品蜜桃| 亚洲AV无码久久精品成人| 久久精品水蜜桃av综合天堂| 精品久久久无码人妻中文字幕豆芽| 国产精品无码久久久久久| 久久精品国产亚洲沈樵| 久久精品亚洲精品国产欧美| 欧美性猛交xxxx免费看久久久| 久久久久99这里有精品10| 亚洲午夜久久久久久久久电影网| 精品久久久久香蕉网| 精品久久久久久无码人妻蜜桃| 色天使久久综合网天天| 久久精品欧美日韩精品| 国产一区二区三精品久久久无广告| 久久久精品人妻无码专区不卡 | 久久久久亚洲爆乳少妇无| 国产精品久久久久久久久软件| 中文字幕久久精品无码| 国产高潮国产高潮久久久91 | 亚洲人成网站999久久久综合 | 99久久精品国内| 亚洲性久久久影院| 国产午夜精品久久久久免费视| 精品一久久香蕉国产线看播放| 久久天天躁狠狠躁夜夜avapp| 色综合久久无码五十路人妻| 国产欧美久久久精品| 久久久久亚洲av成人网人人软件| 99久久无色码中文字幕| 国产69精品久久久久久人妻精品| 91久久福利国产成人精品| 欧洲成人午夜精品无码区久久| 久久最新免费视频|