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

Source: Internet
Author: User

http://breezylee.iteye.com/blog/1340868

Read, write, and update operations on the Java configuration file propertiesBlog Category:
    • Javase
Properties

Read, write, and update operations on the Java configuration file properties
Note: The current project path is
String filepath=system.getproperty ("User.dir");

This is useful for the following programs ...

/**
* Implementation of Read, write, and update operations on Java configuration file 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 {
Path to the properties file
Static String profilepath= "Mail.properties";
/**
* Using static methods
*/
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 string Getkeyvalue (String key) {
return Props.getproperty (key);
}

/**
* Read the value of the primary key according to the primary key "key"
* @param filePath Property file path
* @param 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 is a pair of key values.
* @param keyname Key Name
* @param keyvalue Key value
*/
public static void WriteProperties (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 = 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 list of attributes (key and element pairs) in this properties table to the output stream
Props.store (FOS, "Update" + KeyName + "' value");
} catch (IOException e) {
System.err.println ("Property file update Error");
}
}

/**
* Update the properties file's key-value pairs
* 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 list of attributes (key and element pairs) in this properties table to the output stream
Props.store (FOS, "Update" + 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");
}
}

Another example: The file common.properties and Config classes here are under Class src

Java code
  1. Public static string GetValue (String key) {
  2. try {
  3. InputStream InputStream = Config.  Class.getclassloader (). getResourceAsStream ("common.properties");
  4. Property.load (InputStream);
  5. } catch (Exception e) {
  6. E.printstacktrace ();
  7. }
  8. return (String) property.get (key);
  9. }

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

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.