Java class_property class: Properties

Source: Internet
Author: User

1. Master the use of the Properties class

2. attributes can be stored and read in common and XML files.

Attribute is a common form in a program.

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 and obtain attributes:
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 object
Pro. setProperty ("BJ", "BeiJing"); // set attributes
Pro. setProperty ("TJ", "TianJin ");
Pro. setProperty ("NJ", "NanJing ");
System. out. println ("1. BJ attributes exist:" + pro. getProperty ("BJ "));
System. out. println ("2. The SC property does not exist:" + pro. getProperty ("SC "));
System. out. println ("3. The SC property does not exist. At the same time, set the displayed default value:" + pro. getProperty ("SC", "no "));
}
}; Attribute operations are the preceding setting and reading attributes. Of course, attributes can also be saved in files. The following methods are provided:

Public void Store (OutputStream out, String comments) throws IOException
Write the preceding attributes to the d: \ area. properties file.

Import java. util. Properties;
Import java. io. File;
Import java. io. FileOutputStream;
Import java. io. FileNotFoundException;
Import java. io. IOException;
Public class PropertiesDemo02 {
Public static void main (String args []) {
Properties pro = new Properties (); // create a Properties object
Pro. setProperty ("BJ", "BeiJing"); // set attributes
Pro. setProperty ("TJ", "TianJin ");
Pro. setProperty ("NJ", "NanJing ");
File file = new File ("D:" + File. separator + "area. properteis"); // specify the File to be operated
Try {
Pro. store (new FileOutputStream (file), "Area Info"); // save attributes to common files
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}; At this time, the property content has been saved in the file. Since it can be saved, it can be read.

Public void load (InputStream inStream) throws IOException
Read attribute content using the above method

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 (); // create a Properties object
File file = new File ("D:" + File. separator + "area. properteis"); // specify the File to be operated
Try {
Pro. load (new FileInputStream (file); // read the attribute 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. The SH property exists:" + 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 a Properties object
Pro. setProperty ("BJ", "BeiJing"); // set attributes
Pro. setProperty ("TJ", "TianJin ");
Pro. setProperty ("NJ", "NanJing ");
File file = new File ("D:" + File. separator + "area. xml"); // specify the File to be operated
Try {
Pro. storeToXML (new FileOutputStream (file), "Area Info"); // save attributes to common files
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}; Since the content can be saved in the XML file format, the content can be read using the XML file.

Import java. util. Properties;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileNotFoundException;
Import java. io. IOException;
Public class PropertiesDemo05 {
Public static void main (String args []) {
Properties pro = new Properties (); // create a Properties object
File file = new File ("D:" + File. separator + "area. xml"); // specify the File to be operated
Try {
Pro. loadFromXML (new FileInputStream (file); // read attribute files
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
System. out. println ("1. BJ attributes exist:" + pro. getProperty ("BJ "));
}
}; Summary:

1. If you want to learn more about 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.

Author: "Han shilei programmer column"

Related Article

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.