[Java class set] _ Attribute Class: Properties notes

Source: Internet
Author: User

[Java class set] _ Attribute Class: Properties notes

Objectives of this chapter:

Measure the test taker's knowledge about the usage of the properties class.
Attributes can be saved and read to common and XML files.

A special properties class is provided in the class set to complete attribute operations.

Public class properties extends hashtable <object, Object>

Properties is a subclass of hashtable and also a subclass of map. You can use all MAP operations. However, attribute classes are generally used independently.

Set attributes:
Public object setproperty (string key, string value)

Get attributes:
Public String getproperty (string key)

Public String getproperty (string key, string defaultvalue)

Verify the preceding operations:

Import Java. util. properties; public class propertiesdemo01 {public static void main (string ARGs []) {properties pro = new properties (); // create a properties class pro. setproperty ("BJ", "Beijing"); pro. setproperty ("TJ", "Tianjing"); pro. setproperty ("NJ", "Nanjing"); system. out. println ("1. BJ attributes exist:" + pro. getproperty ("BJ"); system. out. println ("2. SC property does not exist:" + pro. getproperty ("SC"); system. out. println ("3. The SC property does not exist, and the default value is displayed at the same time:" + pro. getproperty ("SC", "no "));}}

The preceding attribute operations are set and read attributes. Of course, you can save the attributes in a file in the attribute operation. The following methods are provided:

Write the following attributes to the D: \ area. properties file.

Import Java. util. properties; import Java. io. file; import Java. io. fileoutputstream; import Java. io. ioexception; public class propertiesdemo02 {public static void main (string ARGs []) {properties pro = new properties (); // create a properties class pro. setproperty ("BJ", "Beijing"); pro. setproperty ("TJ", "Tianjing"); pro. setproperty ("NJ", "Nanjing"); file = new file ("D:" + file. separator + "area. properties "); // specify the file to be operated try {pro. store (New fileoutputstream (file), "area Info"); // Save the attribute to the specified file} catch (ioexception e) {e. printstacktrace ();}}}

Read the properties File

Public void load (inputstream instream) throws ioexception

Import Java. util. properties; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. ioexception; public class propertiesdemo03 {public static void main (string ARGs []) {properties pro = new properties (); file = new file ("D:" + file. separator + "area. properties "); // specify the file to be operated try {pro. load (New fileinputstream (File);} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} system. out. println ("1. BJ attributes exist:" + pro. getproperty ("BJ"); system. out. println ("2. Sh attributes exist:" + pro. getproperty ("sh "));}}

All of the above are saved in common files. In fact, you can save all the content in the XML file during the properties operation.

Import Java. util. properties; import Java. io. file; import Java. io. fileoutputstream; import Java. io. filenotfoundexception; import Java. io. ioexception; public class propertiesdemo04 {public static void main (string ARGs []) {properties pro = new properties (); // create properties object pro. setproperty ("BJ", "Beijing"); // sets the property pro. setproperty ("TJ", "Tianjin"); pro. setproperty ("NJ", "Nanjing"); file = new file ("D:" + file. separator + "area. XML "); // specify the file to be operated try {pro. storetoxml (New fileoutputstream (file), "area Info"); // Save the attribute to a common file} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();}}};

Summary:

1. If you have a better understanding of attribute operations, you can continue to learn about the reflection mechanism and the application of the attribute class.
2. the type of the attribute must be a string, because the operation is the most convenient.
3. attributes can be saved or read to common or XML files. attributes can be expanded to any file in the specified format.

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.