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:
Properties Pro = new properties (); FileInputStream in = new 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:
FileOutputStream ofile = new FileOutputStream (file, "A.properties");p Ro.store (ofile, "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 Import Java.io.BufferedInputStream; 2 Import Java.io.FileInputStream; 3 Import Java.io.FileOutputStream; 4 Import Java.io.InputStream; 5 Import Java.util.Iterator; 6 Import java.util.Properties; 7 8 public class PropertyTest {9 public static void Main (string[] args) {ten Properties prop = new Propertie S (); TRY{12//Read properties file A.properties13 InputStream in = new Bufferedinputstream (New FILEINPUTST Ream ("A.properties")); Prop.load (in); Load Property list iterator<string> it=prop.stringpropertynames (). Iterator (); (It.hasnext ()) {Key=it.next String (); System.out.println (key+ ":" +prop.getproperty (key)); 19 }20 In.close (); 21 22///Save properties to b.properties file FileOutputStream ofile = New FileOutputStream ("B.properties", true);//true means append open prop.setproperty ("Phone", "10086"); PR Op.Store (ofile, "The New properties File"); Ofile.close ();}28 catch (Exception e) {29 System.out.println (e); 30}31} 32}
Java Read-Write properties configuration file "Go"