Tame Tiger: Loading properties from XML

Source: Internet
Author: User
Tags xml parser

The previous version of J2SE 1.5 required that the XML parser be used directly to mount the configuration file and store the 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 to set up and store your programs: LoadFromXML (InputStream is) and Storetoxml (OutputStream OS, String Comment) method.

Basic knowledge of Properties

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.

Listing 1. A set of properties examples

foo=bar
fu=baz

After you load listing 1 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 a property file and list its current set of keys and values. By simply passing the InputStream of this file to the load () method, each key-value pair is added to the Properties instance. The list () is then used to list all attributes or to obtain a separate attribute with GetProperty ().

Listing 2. Mount 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 the key-value pairs in the output of the list () method is different from the order in which they are 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 there is no guarantee of order.

Listing 3. The output of the Loadsample

-- listing properties --
fu=baz
foo=bar
The foo property: bar

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.