1. Implement read, write, and update operations on the Java configuration file properties
PackageCom.config;/*** Implementation of Read, write, and update operations on Java configuration file properties*/ ImportJava.io.BufferedInputStream; ImportJava.io.BufferedReader;ImportJava.io.FileInputStream; Importjava.io.FileNotFoundException; ImportJava.io.FileOutputStream; Importjava.io.IOException; ImportJava.io.InputStream; ImportJava.io.InputStreamReader;ImportJava.io.OutputStream; Importjava.util.Properties; /** * @author * @version */ Public classProperty {//path to the properties file StaticString ProfilePath = System.getproperty ("User.dir") + "/web-inf/configuration.properties"; /*** Using static methods*/ Private StaticProperties props =NewProperties (); Static { Try{InputStream in=NewFileInputStream (ProfilePath); BufferedReader BF=NewBufferedReader (NewInputStreamReader (In, "UTF-8")); Props.load (BF); } Catch(FileNotFoundException e) {e.printstacktrace (); System.exit (-1); } Catch(IOException e) {system.exit (-1); } } /*** Read the value of the corresponding key in the properties file *@paramKey * PRIMARY KEY *@returnString*/ Public Staticstring Getkeyvalue (String key) {returnProps.getproperty (key); } /*** Read the value of the primary key according to the primary key ' key '@paramFilePath Property File path *@paramKey Key Name*/ Public Staticstring ReadValue (String FilePath, String key) {Properties props=NewProperties (); Try{InputStream in=NewBufferedinputstream (NewFileInputStream (FilePath)); Props.load (in); String value=Props.getproperty (key); SYSTEM.OUT.PRINTLN (Key+ "The value of the key is:" +value); returnvalue; } Catch(Exception e) {e.printstacktrace (); return NULL; } } /*** UPDATE (or insert) a pair of properties information (primary key and its key value) * If the primary key already exists, update the value of the primary key; * If the primary key does not exist, the plug-in pair of key values. * @paramkeyname Key Name *@paramkeyvalue Key Value*/ Public Static voidwriteproperties (String keyname,string keyvalue) {Try { //Call the Hashtable method put and use the GetProperty method to provide parallelism. //enforces the use of strings for the keys and values of the properties. The return value is the result of the Hashtable call put. OutputStream fos =NewFileOutputStream (ProfilePath); Props.setproperty (KeyName, keyvalue); //In a format that is appropriate for loading into the Properties table using the Load method,//writes the list of attributes (key and element pairs) in this properties table to the output streamProps.store (FOS, "Update" + KeyName + "' Value")); } Catch(IOException e) {System.err.println ("Property file Update Error"); } } /*** Update the properties file's key value pair * If the primary key already exists, update the value of the primary key; * If the primary key does not exist, the plug-in pair of key values. * @paramkeyname Key Name *@paramkeyvalue Key Value*/ Public voidupdateproperties (String keyname,string keyvalue) {Try{props.load (NewFileInputStream (ProfilePath)); //Call the Hashtable method put and use the GetProperty method to provide parallelism. //enforces the use of strings for the keys and values of the properties. The return value is the result of the Hashtable call put. OutputStream fos =NewFileOutputStream (ProfilePath); Props.setproperty (KeyName, keyvalue); //In a format that is appropriate for loading into the Properties table using the Load method,//writes the list of attributes (key and element pairs) in this properties table to the output streamProps.store (FOS, "Update" + KeyName + "' Value")); } Catch(IOException e) {System.err.println ("Property file Update Error"); } } //Test Code Public Static voidMain (string[] args) {System.out.println (ReadValue (ProfilePath,"Test")); System.out.println (Getkeyvalue ("Test")); System.out.println (Getkeyvalue ("Zhongwen")); WriteProperties ("Mail_server_incoming", "[email protected]"); System.out.println ("Operation Complete"); } }
HttpClient + TestNG Interface Automatic Test fifth chapter