Java Properties class operations, javaproperties class

Source: Internet
Author: User

Java Properties class operations, javaproperties class

Java has an important class: Properties class, which is mainly used to read java configuration files. Configuration files supported by each language. The configuration in java is usually a *. perties file. The file format is text format, the content format is "jian = value" pair format, and the text comment information can be annotated.

Properties inherits Hashtable

 

 

It 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.

Ii. Java reads the Properties File

There are many methods for Java to read the Properties file.

However, the most common method is to use the getResourceAsStream (String name) method of the java. lang. Class, which can be called as follows:

InputStream in = getClass (). getResourceAsStream ("Resource Name ");This is enough for us to write programs.

Or the following is also commonly used:

InputStream in = new BufferedInputStream (new FileInputStream (filepath ));

Iii. related instances

Below are several examples to deepen understanding and memory of the Properties class.

We know that the Java Virtual Machine (JVM) has its own system configuration file (system. properties), which can be obtained through the following methods.

1. Obtain JVM system attributes

public class Property {    public static void main(String args[]){        System.out.println(new Date());        Properties p=System.getProperties();        p.list(System.out);  //print all properties about JVM    }}

2. Read the local configuration file test. properties and Output

Public class Property {public static void main (String args []) {// System. out. println (new Date (); // Properties p = System. getProperties (); // p. list (System. out); // print all properties about JVM Properties text = new Properties (); try {text. load (new FileInputStream ("E: \ workspace \ ch01 \ text. properties ");} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}@ SuppressWarnings ("rawtypes")/* The Enumeration interface is also in java. in the util package, its functions are similar to those of Iterator used to traverse elements in the set, but Enumeration only provides the function of traversing elements in the Vector and Hashtable Collection */Enumeration num = text. propertyNames (); // obtain the Enumeration while (num. hasMoreElements () {String key = (String) num. nextElement (); String value = text. getProperty (key); System. out. println (key + "=" + value );}}}

Result:

age=23name=linyuhuanweight=140long=212

 

3. A comprehensive instance

Read value based on key

Read all properties information

Write new properties information

Package ch01; import java. io. bufferedInputStream; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java. util. enumeration; import java. util. properties;/*** common operations on the Properties class * @ author lin * June 30, 2015 */public class TestProperties {public static void main (String [] args) throws IOException {WriteProperties ("E: \ workspace \ ch01 \ text. properties "," add2 "," 2121 ");} // read value public static String GetValueByKey (String filePath, String key) {Properties pps = new Properties (); try {InputStream in = new BufferedInputStream (new FileInputStream (filePath); pps. load (in); String value = pps. getProperty (key); System. out. println (key + "=" + value); return value;} catch (IOException e) {e. printStackTrace (); return null ;}// read all information in the configuration file @ SuppressWarnings ("rawtypes") public static void GetAllProperties (String filePath) throws IOException {Properties pps = new Properties (); InputStream in = new BufferedInputStream (new FileInputStream (filePath); pps. load (in); Enumeration num = pps. propertyNames (); // obtain all attribute names in the configuration file. enumeration while (num. hasMoreElements () {String key = (String) num. nextElement (); String value = pps. getProperty (key); System. out. println (key + "=" + value) ;}// write Properties information public static void WriteProperties (String filePath, String pKey, String pValue) throws IOException {Properties pps = new Properties (); InputStream in = new FileInputStream (filePath); pps. load (in); OutputStream out = new FileOutputStream (filePath); pps. setProperty (pKey, pValue); pps. store (out, "Update" + pKey + "name ");}}

Result: The test. properties File

#Update add2 name#Tue Jun 30 17:07:55 CST 2015age=23name=linyuhuanweight=140add2=2121long=212

Reference: http://www.cnblogs.com/bakari/p/3562244.html

 

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.