configuration file to read config.properties (UTF-8 format)
# #姓名
name= John
#年龄
age=31
#性别
sex= Man
Java Code
Package com.utils;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import java.util.Properties;
Import Org.apache.commons.lang.StringUtils;
Import Org.apache.log4j.Logger;
/** * Take a value from the properties configuration and print the log, note: The Close method is called manually when reading is complete.
* * @author chichuduxing * @date February 14, 2017 PM 1:45:28/public class Propertiesutils {/** * log object.
* Private static final Logger Logger = Logger.getlogger (Propertiesutils.class);
/** * File name */private String _confname;
Private InputStream _inputstream;
Private Properties _properties;
Public propertiesutils (String confpath) {this._confname = Confpath;
public void Init () throws Exception {_inputstream = new FileInputStream (_confname);
_properties = new properties ();
_properties.load (New InputStreamReader (_inputstream, "UTF-8"));
/** * takes a value from the configuration. * @param key * value @return/private string Gettrimvalue (String key) {String tmp = _properties.getproperty (key);
if (null = = tmp) {return stringutils.empty;
else {return Tmp.trim ();
}/** * takes a nullable string. * * @param key * Value key * @return ""/trim result * * Public String Getstringenempty (string key) {string
TMP = Gettrimvalue (key);
Logger.info ("Read [" + key + "] ==> [" + tmp + "]);
return TMP;
/** * is not an empty string.
* * @param key * Value key * @return ""/trim results * @throws Exception * results are null.
*/public string Getstringdisempty (string key) throws Exception {string tmp = Gettrimvalue (key);
Logger.info ("Read [" + key + "] ==> [" + tmp + "]);
if (Tmp.isempty ()) {throw new Exception ("[+ key +"] value is empty!);}
return TMP;
/** * takes the default value of the string format. * @param key * value key, NOT NULL can * @param defaultvalue * Default value, can be null/public String GETST
Ringvalue (string key, String defaultvalue) {string tmp = Gettrimvalue (key); if (striNgutils.isempty (TMP)) {logger.info ("Read [+ key +"] ==> ["+ tmp +] Isempty,use defalut value:[" + Defaultvalu
E + "]");
TMP = defaultvalue;
else {logger.info ("Read [" + key +]] ==> ["+ tmp +]");
return TMP;
/** * takes an integer, is empty or illegal, throws an exception. * @param key * @param defalutvalue * @return/public int getintvalue (string key) {String tmpvalue = Gettri
Mvalue (key);
int num = 0;
num = Integer.parseint (tmpvalue);
Logger.info ("Read [" + key + "] ==> [" + num +] ");
return num; /** * takes the shape with the default value. Default Value * * @param key * @param defalutvalue * @return/public int getintvalue (STR
ing key, int defalutvalue) {String tmpvalue = Gettrimvalue (key);
int num = 0;
try {num = Integer.parseint (tmpvalue);
Logger.info ("Read [" + key + "] ==> [" + num +] ");
The catch (Exception e) {num = Defalutvalue;
Logger.warn ("Read [" + key + "] ==> [" + Tmpvalue + "], use default value:[" + num + "]); } RetuRN num;
/** the value within the range of *.
* Key that @param key * value.
* @param minimum allowable value for Minnum *.
* The maximum allowable value @param maxnum *.
* @return * @throws Exception * is not a digital/range illegal.
*/public int getintvalue (string key, int minnum, int maxnum) throws Exception {string tmpvalue = Gettrimvalue (key);
int num = 0;
try {num = Integer.parseint (tmpvalue);
if (num >= minnum && num <= maxnum) {//Legal is the end of this.
Logger.info ("Read [" + key + "] ==> [" + num +] ");
return num; The catch (Exception e) {} throw new Exception ("Read [" + key + "] ==> [" + Tmpvalue + "] not in [" + Minnum + "]
, "+ Maxnum +"]);
/** * Take long shaping, with default values. * @param key * @param defalutvalue * @return/public long Getlongvalue (String key, long Defalutvalue) {STR
ing tmpvalue = gettrimvalue (key);
Long num = 0;
try {num = Long.parselong (tmpvalue);
Logger.info ("Read [" + key + "] ==> [" + num +] "); Catch(Exception e)
{num = Defalutvalue;
Logger.warn ("Read [" + key + "] ==> [" + Tmpvalue + "], use default value:[" + num + "]);
return num;
/** the value within the range of *.
* Key that @param key * value.
* @param minimum allowable value for Minnum *.
* The maximum allowable value @param maxnum *.
* @return * @throws Exception * is not a digital/range illegal. */Public long Getlongvalue (String key, long minnum, long Maxnum) throws Exception {String tmpvalue = Gettrimvalue (key
);
Long num = 0;
try {num = Long.parselong (tmpvalue);
if (num >= minnum && num <= maxnum) {//Legal is the end of this.
Logger.info ("Read [" + key + "] ==> [" + num +] ");
return num; The catch (Exception e) {} throw new Exception ("Read [" + key + "] ==> [" + Tmpvalue + "] not in [" + Minnum + "]
, "+ Maxnum +"]);
/** * Closes file stream, requires manual shutdown!
*/public void Close () {if (null!= _inputstream) {try {_inputstream.close ();
The catch (IOException e) {}}}
public static void Main (string[] args) {propertiesutils proputil = new Propertiesutils ("Conf/config.properties");
try {proputil.init ();
Take the value of the key is "Key1", the default is "all" Proputil.getstringvalue ("Key1" and "all");
The value of the key is "name", which cannot be an empty proputil.getstringdisempty ("name");
Proputil.getintvalue ("Age");
Proputil.getstringenempty ("Sex");
catch (Exception e) {e.printstacktrace ();
Configuration file read exception, program exit return;
Finally {//Remember to call the Close Method Proputil.close ();
}
}
}