Java read-write configuration file

Source: Internet
Author: User

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

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.