Detailed Java program read the properties configuration file Method _java

Source: Internet
Author: User
Tags list of attributes

When we write the program, some of the parameters are often changed, and this change is not what we foresee. For example, we developed a module to operate the database, in the development of our connection to the local database so IP, database name, table name, database host and other information is our local, to make this operation of the data module has universality, then the above information can not be written dead in the program. Usually our approach is to use a configuration file to solve the problem.
Various languages have their own profile types that are supported. Like Python, he supports the. ini file. Because he has an internal configparser class to support the reading and writing of the. ini file, the method provided by this class allows programmers to operate the. ini file freely. In Java, Java supports the reading and writing of. Properties files. The JDK built-in Java.util.Properties class facilitates our operation of the. Properties file.

The form of a. properties file

# The following is the server, database information
dbport = localhost 
databaseName = mydb 
dbusername = root 
Dbpassword = root 
# The following is the database table information
dbtable = mytable 
# The following is server information
IP = 192.168.0.9 

In the above file we assume that the file name is: Test.properties file. Where # begins a behavior annotation information, which we call the key on the left side of the equal sign "=" and what we call value on the right side of the equal sign "=". (In fact, we often say that key-value pairs) key should be the variables in our program. And value is based on the actual situation we configure.

Two Properties classes in JDK

The Properties class exists in cell Java.util, which inherits from Hashtable and provides several main methods:
1. GetProperty (String key) to search for properties in this property list with the specified key. That is, the value of the key is obtained by the key of the parameter.
2. Load (InputStream instream), which reads the list of attributes (key and element pairs) from the input stream. Gets all key-value pairs in the file by loading the specified file, such as the Test.properties file above. To search for GetProperty (String key).
3. SetProperty (String key,string value), invoke the Hashtable method put. He sets the value key-value pair by calling the base class's put method.
4. Store (OutputStream out,string comments) to write the list of properties in this properties table (key and element pairs) to the output stream in a format suitable for loading into the properties table using the Load method. In contrast to the load method, this 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.
With these several methods we can operate on the. properties File!

Three. Java Read Properties File sample
There is a properties file Box.properties, which reads as follows:

color=red
name=box
length=18
width=7
heigth=8

Gets the value of the property in which the following code can be used:

InputStream in = null;
Properties P = new properties ();
try {in
  = new Bufferedinputstream (New FileInputStream ("Box.properties"));
  P.load (in);
} catch (FileNotFoundException e) {
  //TODO auto-generated catch block
  e.printstacktrace ();
} catch ( IOException e) {
  //TODO auto-generated catch block
  e.printstacktrace ();
}
enumeration<object> keys = P.keys ();
while (Keys.hasmoreelements ()) {
  string key = (String) keys.nextelement ();
  SYSTEM.OUT.PRINTLN (key + ":" + p.getproperty (key));
}

Or:

InputStream in;
ResourceBundle RB = null;
try {in
  = new Bufferedinputstream (New FileInputStream ("Box.properties"));
  RB = new propertyResourceBundle (in);
} catch (FileNotFoundException E1) {
  //TODO auto-generated catch block
  e1.printstacktrace ();
} catch ( IOException e) {
  //TODO auto-generated catch block
  e.printstacktrace ();
}
if (RB!= null) {
  enumeration<string> keys = Rb.getkeys ();
  while (Keys.hasmoreelements ()) {
    string key = (String) keys.nextelement ();
    SYSTEM.OUT.PRINTLN (key + ":" + rb.getstring (key));
  }

However, the output order is different from the original file.

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.