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