Package com.zqgame.common.utils;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import java.util.Properties;
/**
* @author Anquan
* @desc read/write configuration file .<br>
* @date 2015-3-10 02:30:59
*/
public class Propertyutil {
private static Properties props = null;
private static String Props_url = "Config/adms.properties";
/**
* Read Config file (default path) property value
* @param pro_name--Attribute name
* @return
*/
public static string GetProperty (String pro_name) {
try {
props = new Properties ();
InputStream in = PropertyUtil.class.getClassLoader (). getResourceAsStream (Props_url);
Props.load (in); Load Property List
In.close ();
} catch (IOException e) {
E.printstacktrace ();
}
Return Props.getproperty (Pro_name);
}
/**
* Write config file (default path) property value
* @param map
*/
public static void SetProperty (string k, string v) {
Get absolute path
String FilePath = PropertyUtil.class.getResource ("/" + Props_url). GetPath ();
props = new Properties ();
try {
File File = new file (FilePath);
if (!file.exists ())
File.createnewfile ();
InputStream fis = new FileInputStream (file);
Props.load (FIS);
Be sure to close FIS before modifying the value
Fis.close ();
OutputStream fos = new FileOutputStream (FilePath);
Props.setproperty (k, v);
Props.store (FOS, "update" + V + "' value");
Save, and add comments
Fos.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
/**
* Specify configuration file read
* @param Properties
* @param pro_name
* @return
*/
public static string GetProperty (String properties, String pro_name) {
Props_url = properties;
Return GetProperty (Pro_name);
}
/**
* Specify configuration file Write
* @param Properties
* @param k
* @param v
*/
public static void SetProperty (String properties, String K, string v) {
Props_url = properties;
SetProperty (k, v);
}
public static void Main (string[] args) {
Take value
System.out.println ("pwd=" +propertyutil.getproperty ("password"));
Assign value
Propertyutil.setproperty ("Phone", "18123672594");
System.out.println ("phone=" +propertyutil.getproperty ("Phone"));
}
}
Java read-write configuration file