"Go" Java read-write properties configuration file
1.Properties class and properties configuration file
The properties class inherits from the Hashtable class and implements the map interface, and it also holds the property set in the form of a key-value pair. However, the properties have a special place, that is, its keys and values are string types.
2.main methods in the properties
(1) Load (InputStream instream)
This method can load a property list into the Properties class object from the file input stream corresponding to the. Properties property file. as in the following code:
Newnew fileinputstream ("a.properties");p ro.load (in); In.close ();
(2) Store (OutputStream out, String comments)
This method saves the property list of the Properties class object to the output stream . as in the following code:
New FileOutputStream (file, "a.properties""Comment"); Ofile.close ();
If comments is not empty, the first row of the saved properties file is #comments, which represents the comment information, and if blank, no comment information.
The comment information is followed by the current save time information for the property file.
(3) Getproperty/setproperty
The two methods are to get and set property information, respectively.
3. Code examples
The properties file A.properties as follows:
Name=Rootpass=Liukey=value
Reads the A.properties property list, with the build properties file b.properties. The code is as follows:
1 ImportJava.io.BufferedInputStream;2 ImportJava.io.FileInputStream;3 ImportJava.io.FileOutputStream;4 ImportJava.io.InputStream;5 ImportJava.util.Iterator;6 Importjava.util.Properties;7 8 Public classPropertyTest {9 Public Static voidMain (string[] args) {TenProperties prop =NewProperties (); One Try{ A //read the properties file A.properties -InputStream in =NewBufferedinputstream (NewFileInputStream ("A.properties")); -Prop.load (in);///Load Property list theIterator<string> it=prop.stringpropertynames (). iterator (); - while(It.hasnext ()) { -String key=It.next (); -System.out.println (key+ ":" +Prop.getproperty (key)); + } - in.close (); + A ///Save properties to B.properties file atFileOutputStream ofile =NewFileOutputStream ("B.properties",true);//true indicates append open -Prop.setproperty ("Phone", "10086"); -Prop.store (Ofile, "The New properties File"); - ofile.close (); - } - Catch(Exception e) { in System.out.println (e); - } to } +}
"Go" Java read-write properties configuration file