Java programming in the properties of the specific role and use!

Source: Internet
Author: User

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. (as shown in Listing 1). The recently updated Java.util.Properties class now provides an easier way for program loading and storage settings: LoadFromXML (Inputstreamis) and Storetoxml (OutputStream OS, String Comment) method.

Here is a detailed explanation, hoping to bring you help.

Listing 1. A set of property examples

Foo=bar
Fu=baz

After listing 1 is loaded into the 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 the properties file and list out 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.

Listing 2. Load Properties

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

<?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

<?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

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

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

<?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>
An example is changed here:
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.Properties;
/**
* Implementation of properties file reading
* @author Haoxuewu
*/
public class Test {
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"));
Long end = System.currenttimemillis ();
System.out.println ("Cost:" + (End-start));
} catch (IOException IoE) {
Ioe.printstacktrace ();
}
}
}
Conf.properties
# Configuration File
Homepage = Http://www.blogjava.net/haoxuewu
Author = bbflyerwww
School = Jilinjianzhugongchengxueyuan

Result
Size:3
Homepage:http://www.blogjava.net/haoxuewu
Author:bbflyerwww
School:jilinjianzhugongchengxueyuan

Java programming in the properties of the specific role and use!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.