/** * Implement read, write, and update to the Java profile properties */package test; Import Java.io.BufferedInputStream; Import Java.io.FileInputStream; Import java.io.FileNotFoundException; Import Java.io.FileOutputStream; Import java.io.IOException; Import Java.io.InputStream; Import Java.io.OutputStream; Import java.util.Properties; /** * @author * @version * */public class Setsystemproperty {//Properties file path static String profilepath= "Mail.properties" ; /** * Use static method */private static Properties props = new properties (); static {try {props.load (new FileInputStream (ProfilePath)); } 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 * @param key * PRIMARY KEY * @return String */public static Stri Ng Getkeyvalue (String key) {return Props.getproperty (key); }/** * Based on primary keyKey reads the value of the primary key * @param filePath Property file path * @param key Key Name */public static string ReadValue (String FilePath, String key) {Properties props = new Properties (); try {inputstream in = new Bufferedinputstream (new FileInputStream (FilePath)); Props.load (in); String value = Props.getproperty (key); SYSTEM.OUT.PRINTLN (key + "key value is:" + value); return value; } 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. * @param keyname Key name * @param keyvalue Key value */public static void WriteProperties (String keyname,string keyvalue) {try {//Call Hashtable method Put, 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 = new FileOutputStream (ProfilePath); Props.setproperty (KeyName, keyvalue); In a format that is appropriate for loading into the properties table using the Load method,//Writes the attribute list (key and element pairs) in this properties table to the output stream Props.store (fos, "updat E ' "+ 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 is a pair of key values. * @param keyname Key name * @param keyvalue Key value */public void updateproperties (String keyname,string keyvalue) { try {props.load (new FileInputStream (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 = new FileOutputStream (ProfilePath); Props.setproperty (KeyName, keyvalue); In a format that is appropriate for loading into the properties table using the Load method,//Writes the attribute list (key and element pairs) in this properties table to the output stream Props.store (fos, "updat E ' "+ KeyName +" ' value "); } catch (IOException e) {System.err.println ("Property file update Error"); }}//test code public static void Main (string[] args) {readvalue ("mail.properties", "Mail_server_password "); WriteProperties ("mail_server_incoming", "[email protected]"); SYSTEM.OUT.PRINTLN ("Operation completed"); } }
Read, write, and update operations for the Java configuration file properties