Java properties Read and set values

Source: Internet
Author: User
Tags list of attributes

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.Enumeration;
Import Java.util.HashMap;
Import Java.util.Map;
Import java.util.Properties;

/**
* Read the Properties synthesis class, which is bound to the Config.properties file under Classpath by default.
* */
public class Propertiesutil {
Path to the configuration file
Private String Configpath=null;

/**
* Configuration File Object
*/
Private Properties Props=null;

/**
* Default constructor for SH run, automatically find Classpath under Config.properties.
*/
Public Propertiesutil () throws ioexception{
InputStream in = PropertiesUtil.class.getClassLoader (). getResourceAsStream ("config.properties");
props = new Properties ();
Props.load (in);
Close Resource
In.close ();
}

/**
* Read the configured value according to the key value
* June, 9:15:43 PM
* @param key key value
* @return the value corresponding to the key key
* @throws IOException
*/
public string ReadValue (string key) throws IOException {
return Props.getproperty (key);
}

/**
* Read all information about the properties
* June, 9:21:01 PM
* @throws filenotfoundexception configuration file not found
* @throws IOException Close the resource file or load the configuration file incorrectly
*
*/
Public map<string,string> readallproperties () throws Filenotfoundexception,ioexception {
Save all the key values
Map<string,string> map=new hashmap<string,string> ();
Enumeration en = Props.propertynames ();
while (En.hasmoreelements ()) {
String key = (string) en.nextelement ();
String property = Props.getproperty (key);
Map.put (key, property);
}
return map;
}

/**
* Set the value of a key and save it to a file.
* June, 9:15:43 PM
* @param key key value
* @return the value corresponding to the key key
* @throws IOException
*/
public void SetValue (String key,string value) throws IOException {
Properties prop = new properties ();
InputStream fis = new FileInputStream (This.configpath);
Read the list of attributes from the input stream (key and element pairs)
Prop.load (FIS);
Call the 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 (This.configpath);
Prop.setproperty (key, value);
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
Prop.store (FOS, "last Update");
Close File
Fis.close ();
Fos.close ();
}

public static void Main (string[] args) {
Propertiesutil p;
try {
p = new Propertiesutil ();
System.out.println (P.readallproperties ());
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}

Java properties Read and set values

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.