http://gimgen1026.iteye.com/blog/152023
The
Properties class is no longer new, it has been in early Java programming and almost nothing has changed. The Tiger version of J2SE enhances this class by specifying multiple key-value pairs separated by an equal sign in a single line, as well as loading and saving these key-value pairs with an XML file. In this issue of taming Tiger, John Zukowski shows how to harness this new generation of "horses". The previous versions of
J2SE 1.5 required that the configuration file be loaded directly using the XML parser and stored settings. While this is not a difficult task, and the parser is the standard part of the platform, the extra work is always a bit annoying. The recently updated Java.util.Properties class now provides an easier way for program loading and storage settings: LoadFromXML (InputStream is) and Storetoxml (OutputStream OS, String Comment) method.
Properties Basics
If you are unfamiliar with the Java.util.Properties class, you are now told that it is used to store key-value pairs in a file where the keys and values are separated by an equal sign, This is shown in Listing 1.
Listing 1. A set of property examples
Foo=bar
Fu=baz
Load Listing 1 into Properties object, you can find two keys (foo and FU) and two values (Foo's Bar and Fu's Baz). This class supports embedded Unicode strings with \u, but what is important here is that each item is treated as a string.
Listing 2 shows how to load a property file and list its current set of keys and values. Simply pass the inputstream of this file to the load () method, and each key-value pair will be added to the Properties instance. Then list all attributes with list () or use GetProperty () to get individual properties.
Load Properties
Java code
- 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"));
- }
- }
Run the Loadsample program to generate the output shown in Listing 3. Note The order of key-value pairs in the output of the list () method is different from the order in which they are entered in the input file. The Properties class stores a set of key-value pairs in a hash table (hashtable, in fact, a Hashtable subclass), so the order is not guaranteed.
Listing 3. Output of the Loadsample
--Listing properties--
Fu=baz
Foo=bar
The Foo Property:bar
XML Properties File
There's nothing new here. The Properties class always works like this. However, the new place is to load a set of properties from an XML file. Its DTD is shown in Listing 4.
Listing 4. Attribute DTD
DTD writes <?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 >
If you do not want to read the XML DTD, you can tell you that it is a <comment> tag wrapped in the perimeter <properties> tag, followed by any number of <entry> tags. For each <entry> tag, there is a key attribute, and the input content is its value. Listing 5 shows what the XML version of the properties file in Listing 1 looks like.
Listing 5. XML version of the properties file
Java code
- <?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>
As shown in Listing 6, reading the XML version of the Properties file is no different from reading the old format file.
Listing 6. Read the XML Properties file
Java code
- 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"));
- }
- }
A description of resource bindings
Although the Java.util.Properties class now supports the property file as an XML file in addition to the key-value pairs, unfortunately, there is no built-in option to handle ResourceBundle as an XML file. Yes, propertyResourceBundle does not use the Properties object to load the bindings, but the use of the Load method is hardcoded into the class without using the newer LoadFromXML () method.
Running the program in Listing 6 produces the same output as the original program, as shown in Listing 2.
Saving XML attributes
Another feature of the new properties is the ability to store properties in XML-formatted files. Although the store () method will still create a file like the one shown in Listing 1, you can now create the file as shown in Listing 5 with the new Storetoxml () method. Just pass a outputstream and a String for the comment. Listing 7 shows the new Storetoxml () method.
Listing 7. Store Properties as XML files
Java code
- 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 ();
- }
- }
Run the output from the program in Listing 7 as shown in Listing 8.
Listing 8. The stored XML file
Java code
- <?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>
Conclusion
Using an XML file or using an old-fashioned a=b type of file is entirely up to you. Old-fashioned files are certainly lightweight from a memory point of view. However, because of the widespread use of XML, it is expected that the XML format will become popular because it has been widely used, but not the Properties object. Choose exactly where you are. Analyze the source code for the private Xmlutils class of the package to get more information about the XML parsing being used.
Java code
- Import Java.io.FileInputStream;
- Import java.io.IOException;
- Import Java.io.InputStream;
- Import java.util.Properties;
- /**
- * Implementation of properties file reading
- * @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 code
- # Configuration Fileauthor = bbflyerwww
- School = WuHan University
- Date = 2006-08-
Result
Java.util.properties