Operation of the properties class in Java

Source: Internet
Author: User

Java has a relatively important class properties (Java.util.Properties), mainly used to read the Java configuration file, just like the database account password, its configuration file is usually a. Properties file, in the form of a text file, The format of the contents of the file is the "key = value" format, and the text annotation information can be annotated with "#".

The properties class inherits from Hashtable, as follows:

It provides several main methods:

1. GetProperty (String key) that searches for properties in this property list with the specified key. That is, by the parameter key, the value corresponding to the key is obtained.

2. load (InputStream instream) to read the list of attributes (key and element pairs) from the input stream. Gets all the key-value pairs in the file by loading the specified file (for example, the following test.properties file). To search for GetProperty (String key).

3. SetProperty (string key, String value), calls the Hashtable method put. He sets a key-value pair by calling the put method of the base class.

4. Store (OutputStream out, String comments) writes the list of attributes (key and element pairs) in this properties table to the output stream, in a format that is appropriate for loading into the properties table using the Load method. In contrast to the load method, the method writes a key-value pair to the specified file.

5. Clear () Clears all loaded key-value pairs. This method is provided in the base class.

If you want to use properties, you can simply declare the code in the method or class:

Properties P = new properties ();

A few examples are listed below:

Create a new configuration file (test.properties)

name=jjweight=4444height=3333
1  Public classgetProperties {2      Public Static voidMain (string[] args)throwsFileNotFoundException, IOException {3Properties pps =NewProperties ();4Pps.load (NewFileInputStream ("Test.properties"));5Enumeration enum1 = Pps.propertynames ();//get the name of the configuration file6          while(Enum1.hasmoreelements ()) {7String strkey =(String) enum1.nextelement ();8String strvalue =Pps.getproperty (strkey);9System.out.println (strkey + "=" +strvalue);Ten         } One     } A}

Note, however, that when using, be sure to configure the relevant code inside the configuration to scan this . Properties end of the file.

Welcome to join the Java Technology Group for discussion, point me into the group

Operation of the properties class in Java

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.