First, Lang order
In Java.The properties file is a configuration file that is used primarily to express configuration information with a file type of *.Properties, formatted as text files, the contents of the file are formatted as "key = value" in the format,Properties file, you can use "#" to make comments,Properties file onJava programming in a lot of places, easy to operate.
------------------------------------------------------
Java.util, the properties class inherits from the Hashtable
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. Through the specified file (for example, the above test.Properties file) to get all the key-value pairs in the file for GetProperty (String key) search.
3. SetProperty (string key, String value), calls the Hashtable method of the Put method. It sets the key-value pair by calling the put method of the base class.
4. Store (OutputStream out, String comments) writes the output stream to a list of attributes (key and element pairs) that are loaded 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, the Operation Java methods for properties files
PackageCom.hibernate.utils.util1;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map;Importjava.util.Properties;ImportJava.util.Set; Public Final classPropertiesutil {/*** Read the value of a property*/ Public Staticstring GetProperty (String fileName, String propertyname) {Properties prop=NewProperties (); InputStream in=NULL; Try{ in=NewFileInputStream (GetFilePath () +fileName); Prop.load (in); String PropertyValue=Prop.getproperty (PropertyName); returnPropertyValue; } Catch(IOException e) {e.printstacktrace (); return NULL; } finally { Try{in.close (); } Catch(IOException e) {e.printstacktrace (); } } } /*** Add or modify attributes*/ Public Static voidSaveorupdateproperty (String fileName, String propertyname, String propertyvalue) {Map<string, string> tosavemap =getProperties (fileName); Tosavemap.put (PropertyName, PropertyValue);//Add or update propertiesProperties prop =NewProperties (); OutputStream out=NULL; Try{ out=NewFileOutputStream (GetFilePath () +fileName); } Catch(FileNotFoundException e) {e.printstacktrace (); } prop.putall (Tosavemap); Try{Prop.store (out,"Come from Add Or Update action");//Update Properties File}Catch(IOException e) {e.printstacktrace (); } } /*** Delete Attributes*/ Public Static voidDeleteProperty (String fileName, String propertyname) {Map<string, string> tosavemap =getProperties (fileName); Tosavemap.remove (PropertyName);//Removing properties fromProperties prop =NewProperties (); OutputStream out=NULL; Try{ out=NewFileOutputStream (GetFilePath () +fileName); } Catch(FileNotFoundException e) {e.printstacktrace (); } prop.putall (Tosavemap); Try{Prop.store (out,"");//Update Properties File}Catch(IOException e) {e.printstacktrace (); } } /*** Gets the absolute path where the class resides*/ Public StaticString GetFilePath () {returnPropertiesutil.class. GetClass (). GetResource ("/"). GetPath (); } /*** Read all attributes of the properties file*/ Public StaticMap<string, string>getProperties (String fileName) {Properties prop=NewProperties (); Try{InputStream in=NewFileInputStream (GetFilePath () +fileName); Prop.load (in);//Load Property values}Catch(IOException e) {e.printstacktrace (); } Map<string, string> tosavemap =NewHashmap<string, string>(); Set<Object> keys =Prop.keyset (); Iterator<Object> it =Keys.iterator (); while(It.hasnext ()) {String key=(String) it.next (); String value=(String) prop.get (key); Tosavemap.put (key, value); } returnTosavemap; }}
----------------------------------------Youth----------------------------------------
Java Operations Properties File