There is a more important class in Java: The Properties class, which is primarily used to read Java configuration files. Each language is supported by its own configuration file. The configuration in Java is usually the *.perperties file, the file format is text format, the content format is "health = value" pair format, the text annotation information can be used # comment.
Properties Inheritance Hashtable
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 Test.properties file above). 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.
Second, Java read the properties file
There are many ways that Java reads the properties file
But the most common is implemented by the Java.lang.Class class's getResourceAsStream (String name) method, which can be called as follows:
InputStream in = GetClass (). getResourceAsStream ("Resource Name"); As we write the program, use this one enough.
Or the following is commonly used:
InputStream in = new Bufferedinputstream (new FileInputStream (filepath));
Third, relevant examples
Here are a few examples to deepen the understanding and memory of the properties class.
We know that the Java Virtual Machine (JVM) has its own system configuration file (System.properties), which we can obtain in the following way.
1. Get the system properties of the JVM
Public class Property { publicstaticvoid 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 classProperty { Public Static voidMain (String args[]) {//System.out.println (New Date ());//Properties p=system.getproperties ();//p.list (System.out); //Print all properties about JVMProperties Text=NewProperties (); Try{text.load (NewFileInputStream ("E:\\workspace\\ch01\\text.properties")); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } @SuppressWarnings ("Rawtypes") /*The enumeration interface is also in the Java.util package, and its functions and iterator are almost always used to traverse the elements in the collection, but the enumeration enumeration only provides the ability to traverse the vector and Hashtable type collection elements */Enumeration Num=text.propertynames ();//get the enumeration about key in the contents of the accessory file while(Num.hasmoreelements ()) {String key=(String) num.nextelement (); String value=Text.getproperty (key); SYSTEM.OUT.PRINTLN (Key+ "=" +value); } }}
Results:
age=23Name=linyuhuanweight=140long=212
3, a more comprehensive example
Read value by key
Read all information about the properties
Write a new properties information
Packagech01;ImportJava.io.BufferedInputStream;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;Importjava.util.Enumeration;Importjava.util.Properties;/*** Common operations on the properties class *@authorLin * June 30, 2015*/ Public classtestproperties { Public Static voidMain (string[] args)throwsioexception{WriteProperties ("E:\\workspace\\ch01\\text.properties", "Add2", "2121"); } //read value by key Public Staticstring Getvaluebykey (String filepath,string key) {Properties pps=NewProperties (); Try{InputStream in=NewBufferedinputstream (NewFileInputStream (FilePath)); Pps.load (in); String value=Pps.getproperty (key); SYSTEM.OUT.PRINTLN (Key+ "=" +value); returnvalue; }Catch(IOException e) {e.printstacktrace (); return NULL; } } //Read all information about a configuration file@SuppressWarnings ("Rawtypes") Public Static voidGetallproperties (String FilePath)throwsioexception{Properties pps=NewProperties (); InputStream in=NewBufferedinputstream (NewFileInputStream (FilePath)); Pps.load (in); Enumeration Num= Pps.propertynames ();//gets all the property 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 voidWriteProperties (String filepath,string pkey,string pValue)throwsIOException {Properties pps=NewProperties (); InputStream in=NewFileInputStream (FilePath); Pps.load (in); OutputStream out=NewFileOutputStream (FilePath); Pps.setproperty (PKey, PValue); Pps.store (out,"Update" + PKey + "name"); }}
Results: Test.properties file
17:07:55 CST =23name=linyuhuanweight=140add2=2121long=212
Reference Link: http://www.cnblogs.com/bakari/p/3562244.html
Actions of the properties class in Java