Basic usage of the property class in Java

Source: Internet
Author: User

I have never noticed that the configuration file can be well written.

From: http://hi.baidu.com/ai5173609/blog/item/30e1a28fb6de21fe503d927d.html

All languages have their own Supported configuration file types. For example, Python supports. ini files. Because it has a configparser class to support reading and writing. ini files, according to the method provided by this classProgramYou can operate the. ini file as needed. In Java, JAVA supports reading and writing the. properties file. The built-in JAVA. util. properties class of JDK provides convenience for us to operate on the. properties file.

1. properties File Format

# Server and database information

Dbport = localhost
Databasename = mydb
Dbusername = root
Dbpassword = root

# The following table information

Dbtable = mytable

# Server Information

IP = 192.168.0.9

In the preceding file, assume that the file name is test. properties. Where # the start line is the comment information. The key on the left side of the equal sign "=" is called the key, and the value on the right side of the equal sign "=" is called the value. (In fact, we often say that the key-Value Pair) key should be a variable in our program. Value is configured according to the actual situation.

2. The properties class of JDK exists in the cell java. util, which inherits from hashtable and provides several main methods:
1. getproperty (string key). Use the specified key to search for properties in this attribute list. That is, the value corresponding to the key is obtained through the key parameter.

2. Load (inputstream instream), read the attribute list (key and element pair) from the input stream ). You can load the specified file (such as the test. properties File above) to obtain all key-value pairs in the file. For getproperty (string key) search.

3. setproperty (string key, string value): Call the hashtable method put. He sets key-value pairs by calling the PUT Method of the base class.

4. Store (outputstream out, string comments) writes the attribute list (key and element pair) in the properties table to the output stream in a format suitable for loading to the properties table using the load method. In contrast to the load method, this method writes a key-value pair to a specified file.

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

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.