Java properties, properties

Source: Internet
Author: User

Java properties, properties

I. Java Properties class

Java has an important class Properties (Java. util. properties), mainly used to read Java configuration files. Various languages have their own Supported configuration files. Many variables in the configuration files are often changed, this allows you to modify related variable settings out of the program itself. The configuration file supported by Python is A. ini file. Similarly, it also has its own ConfigParse class for reading the configuration file to facilitate programmers or users to modify the. ini configuration file through this class. In Java, the configuration file is usually. properties file, in the format of text files. The file content is in the format of "Key = value". You can use "#" to annotate text comments.

The Properties class is inherited from Hashtable, as follows:

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. For details, refer to the six 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 sufficient for writing 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

1 import java.util.Properties;2 3 public class ReadJVM {4     public static void main(String[] args) {5         Properties pps = System.getProperties();6         pps.list(System.out);7     }8 }

Result:

 

2. Create a configuration file (Test. properties)

name=JJWeight=4444Height=3333
1 public class getProperties {2 public static void main (String [] args) throws FileNotFoundException, IOException {3 Properties pps = new Properties (); 4 pps. load (new FileInputStream ("Test. properties "); 5 Enumeration enum1 = pps. propertyNames (); // get the name of the configuration file 6 while (enum1.hasMoreElements () {7 String strKey = (String) enum1.nextElement (); 8 String strValue = pps. getProperty (strKey); 9 System. out. println (strKey + "=" + strValue); 10} 11} 12}


3. A comprehensive instance

Read value based on key

Read all properties information

Write new properties information

1 // common Properties operations 2 public class TestProperties {3 // read Value 4 public static String GetValueByKey (String filePath, String Key) based on the key) {5 Properties pps = new Properties (); 6 try {7 InputStream in = new BufferedInputStream (new FileInputStream (filePath); 8 pps. load (in); 9 String value = pps. getProperty (key); 10 System. out. println (key + "=" + value); 11 return value; 12 13} catch (IOException e) {14 e. printStackTrace (); 15 return null; 16} 17} 18 19 // read all Properties information 20 public static void GetAllProperties (String filePath) throws IOException {21 Properties pps = new Properties (); 22 InputStream in = new BufferedInputStream (new FileInputStream (filePath); 23 pps. load (in); 24 Enumeration en = pps. propertyNames (); // get the configuration file name 25 26 while (en. hasMoreElements () {27 String strKey = (String) en. next Element (); 28 String strValue = pps. getProperty (strKey); 29 System. out. println (strKey + "=" + strValue); 30} 31 32} 33 34 // write Properties information 35 public static void WriteProperties (String filePath, String pKey, String pValue) throws IOException {36 Properties pps = new Properties (); 37 38 InputStream in = new FileInputStream (filePath); 39 // read the attribute list (key and element pair) from the input stream 40 pps. load (in); 41 // call the Hashtable method put. Use the getProperty method to provide parallelism. 42 // The attribute key and value must use a string. The returned value is the result of calling put by Hashtable. 43 OutputStream out = new FileOutputStream (filePath); 44 pps. setProperty (pKey, pValue); 45 // in a format suitable for loading data to the Properties table using the load method, 46 // list the Properties in the Properties table (key and element pair) write output stream 47 pps. store (out, "Update" + pKey + "name"); 48} 49 50 public static void main (String [] args) throws IOException {51 // String value = GetValueByKey ("Test. properties "," name "); 52 // System. out. println (value); 53 // GetAllProperties ("Test. properties "); 54 WriteProperties (" Test. properties "," long "," 212 "); 55} 56}

Result:
The file data in Test. properties is:

#Update long name#Sun Feb 23 18:17:16 CST 2014name=JJWeight=4444long=212Height=3333

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.