java.util.properties 类的使用

  • Post author:
  • Post category:java




Properties 基本知识


如果不熟悉 java.util.Properties 类,那么现在告诉您它是用来在一个文件中存储键-值对的,其中键和值是用等号分隔的,如清单 1 所示。



清单 1.


一组属性示例

foo=bar

fu=baz

将清单 1 装载到 Properties 对象中后,您就可以找到两个键( foo 和 fu )和两个值( foo 的 bar 和 fu 的 baz )了。这个类支持带 \u 的嵌入 Unicode 字符串,但是这里重要的是每一项内容都当作 String 。

清单 2 显示了如何装载属性文件并列出它当前的一组键和值。只需传递这个文件的 InputStream 给 load() 方法,就会将每一个键-值对添加到 Properties 实例中。然后用 list() 列出所有属性或者用 getProperty() 获取单独的属性。



清单 2.


装载属性


Java代码



  1. import

    java.util.*;


  2. import

    java.io.*;



  3. public


    class

    LoadSample {


  4. public


    static


    void

    main(String args[])

    throws

    Exception {

  5. Properties prop =

    new

    Properties();

  6. FileInputStream fis =


  7. new

    FileInputStream(

    “sample.properties”

    );

  8. prop.load(fis);

  9. prop.list(System.out);

  10. System.out.println(

    “\nThe foo property: ”

    +

  11. prop.getProperty(

    “foo”

    ));

  12. }

  13. }
import java.util.*;
import java.io.*;

public class LoadSample {
    public static void main(String args[]) throws Exception {
      Properties prop = new Properties();
      FileInputStream fis = 
        new FileInputStream("sample.properties");
      prop.load(fis);
      prop.list(System.out);
      System.out.println("\nThe foo property: " +
          prop.getProperty("foo"));
    }
}



运行 LoadSample 程序生成如清单 3 所示的输出。注意 list() 方法的输出中键-值对的顺序与它们在输入文件中的顺序不一样。 Properties 类在一个散列表(hashtable,事实上是一个 Hashtable 子类)中储存一组键-值对,所以不能保证顺序。








清单 3.


LoadSample 的输出

— listing properties —

fu=baz

foo=bar

The foo property: bar


XML 属性文件


这里没有什么新内容。 Properties 类总是这样工作的。不过,新的地方是从一个 XML 文件中装载一组属性。它的 DTD 如清单 4 所示。



清单 4.


属性 DTD


dtd 写道

<?xml version=”1.0″ encoding=”UTF-8″?>

<!– DTD for properties –>

<!ELEMENT properties ( comment?, entry* ) >

<!ATTLIST properties version CDATA #FIXED “1.0”>

<!ELEMENT comment (#PCDATA) >

<!ELEMENT entry (#PCDATA) >

<!ATTLIST entry key CDATA #REQUIRED>

如果不想细读 XML DTD,那么可以告诉您它其实就是说在外围 <properties> 标签中包装的是一个 <comment> 标签,后面是任意数量的 <entry> 标签。对每一个 <entry> 标签,有一个键属性,输入的内容就是它的值。清单 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>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Hi</comment>
<entry key="foo">bar</entry>
<entry key="fu">baz</entry>
</properties>



如果清单 6 所示,读取 XML 版本的 Properties 文件与读取老格式的文件没什么不同。



清单 6.


读取 XML Properties 文件


Java代码

复制代码


收藏代码




  1. import

    java.util.*;


  2. import

    java.io.*;



  3. public


    class

    LoadSampleXML {


  4. public


    static


    void

    main(String args[])

    throws

    Exception {

  5. Properties prop =

    new

    Properties();

  6. FileInputStream fis =


  7. new

    FileInputStream(

    “sampleprops.xml”

    );

  8. prop.loadFromXML(fis);

  9. prop.list(System.out);

  10. System.out.println(

    “\nThe foo property: ”

    +

  11. prop.getProperty(

    “foo”

    ));

  12. }

  13. }
import java.util.*;
import java.io.*;

public class LoadSampleXML {
    public static void main(String args[]) throws Exception {
      Properties prop = new Properties();
      FileInputStream fis =
        new FileInputStream("sampleprops.xml");
      prop.loadFromXML(fis);
      prop.list(System.out);
      System.out.println("\nThe foo property: " +
          prop.getProperty("foo"));
    }
}


关于资源绑定的说明


虽然 java.util.Properties 类现在除了支持键-值对,还支持属性文件作为 XML 文件,不幸的是,没有内置的选项可以将 ResourceBundle 作为一个 XML 文件处理。是的, PropertyResourceBundle 不使用 Properties 对象来装载绑定,不过装载方法的使用是硬编码到类中的,而不使用较新的 loadFromXML() 方法。

运行清单 6 中的程序产生与原来的程序相同的输出,如 清单 2所示。





保存 XML 属性


新的 Properties 还有一个功能是将属性存储到 XML 格式的文件中。虽然 store() 方法仍然会创建一个类似 清单 1 所示的文件,但是现在可以用新的 storeToXML() 方法创建如 清单 5 所示的文件。只要传递一个 OutputStream 和一个用于注释的 String 就可以了。清单 7 展示了新的 storeToXML() 方法。



清单 7.


将 Properties 存储为 XML 文件


Java代码

复制代码


收藏代码




  1. import

    java.util.*;


  2. import

    java.io.*;



  3. public


    class

    StoreXML {


  4. public


    static


    void

    main(String args[])

    throws

    Exception {

  5. Properties prop =

    new

    Properties();

  6. prop.setProperty(

    “one-two”

    ,

    “buckle my shoe”

    );

  7. prop.setProperty(

    “three-four”

    ,

    “shut the door”

    );

  8. prop.setProperty(

    “five-six”

    ,

    “pick up sticks”

    );

  9. prop.setProperty(

    “seven-eight”

    ,

    “lay them straight”

    );

  10. prop.setProperty(

    “nine-ten”

    ,

    “a big, fat hen”

    );

  11. FileOutputStream fos =


  12. new

    FileOutputStream(

    “rhyme.xml”

    );

  13. prop.storeToXML(fos,

    “Rhyme”

    );

  14. fos.close();

  15. }

  16. }
import java.util.*;
import java.io.*;

public class StoreXML {
    public static void main(String args[]) throws Exception {
      Properties prop = new Properties();
      prop.setProperty("one-two", "buckle my shoe");
      prop.setProperty("three-four", "shut the door");
      prop.setProperty("five-six", "pick up sticks");
      prop.setProperty("seven-eight", "lay them straight");
      prop.setProperty("nine-ten", "a big, fat hen");
      FileOutputStream fos =
        new FileOutputStream("rhyme.xml");
      prop.storeToXML(fos, "Rhyme");
      fos.close();
    }
}

运行清单 7 中的程序产生的输出如清单 8 所示。



清单 8.


存储的 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>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Rhyme</comment>
<entry key="seven-eight">lay them straight</entry>
<entry key="five-six">pick up sticks</entry>
<entry key="nine-ten">a big, fat hen</entry>
<entry key="three-four">shut the door</entry>
<entry key="one-two">buckle my shoe</entry>
</properties>


结束语


使用 XML 文件还是使用老式的 a=b 类型的文件完全取决于您自己。老式文件从内存的角度看肯定是轻量级的。不过,由于 XML 的普遍使用,人们会期望 XML 格式流行起来,因为它已经被广泛使用了,只不过没有用到 Properties 对象。选择完全在您。分析软件包 private XMLUtils 类的源代码以获得关于所使用的 XML 解析的更多信息。


Java代码



  1. import

    java.io.FileInputStream;


  2. import

    java.io.IOException;


  3. import

    java.io.InputStream;


  4. import

    java.util.Properties;


  5. /**



  6. * 实现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. }
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 实现properties文件的读取
* @author bbflyerwww
* @date 2006-08-02
*/
public class PTest {
      public static void main(String[] args) {
          try {
              long start = System.currentTimeMillis();
              InputStream is = new FileInputStream("conf.properties");
              Properties p = new Properties();
              p.load(is);
              is.close();
              System.out.println("SIZE : " + p.size());
              System.out.println("homepage : " + p.getProperty("homepage"));
              System.out.println("author : " + p.getProperty("author"));
              System.out.println("school : " + p.getProperty("school"));
              System.out.println("date : " + p.getProperty("date"));
              long end = System.currentTimeMillis(); 
              System.out.println("Cost : " + (end - start));
          } catch (IOException ioe) {
              ioe.printStackTrace();
          }
      }
}

conf.properties


Java代码


  1. # Configuration fileauthor = bbflyerwww

  2. school = WuHan University

  3. date =

    2006



    08



    02

# Configuration fileauthor = bbflyerwww
school = WuHan University
date = 2006-08-02

Result


  • SIZE:4

    author : bbflyerwww

    school : WuHan University

    date : 2006-08-02

    Cost : 0