Read, write, and update operations for the Java configuration file properties

Source: Internet
Author: User

/** * 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.