展开全部
import java.util.Properties;
public class PropertiesUtil {
private static Properties init = null;
private static Properties util = null;
private static Properties chid = null;
private synchronized static Properties getInit(){
if(init == null){
try{
init = new Properties();
init.load(PropertiesUtil.class.getClassLoader().getResourceAsStream("init.properties"));
}catch(Exception e){
e.printStackTrace();
}
}
return init;
}
private synchronized static Properties getUtil(){
if(util == null){
try{
util = new Properties();
util.load(PropertiesUtil.class.getClassLoader().getResourceAsStream("util.properties"));
}catch(Exception e){
e.printStackTrace();
}
}
return util;
}
private synchronized static Properties getChid(){
if(chid == null){
try{
chid = new Properties();
chid.load(PropertiesUtil.class.getClassLoader().getResourceAsStream("chid.properties"));
}catch(Exception e){
e.printStackTrace();
}
}
return chid;
}
/**
* 获取属性配置文件参数值
* @param key 参数名称32313133353236313431303231363533e4b893e5b19e31333363393564
* @param def 参数默认值
* @return 参数值
*/
public static String get(String key, String def){
String val = getInit().getProperty(key);
if(val == null || val.length() == 0){
return def;
}
return val;
}
public static long getlong(String key,long def)
{
try{
def = Long.parseLong(getInit().getProperty(key));
}catch(Exception e){
e.printStackTrace();
return def;
}
return def;
}
/**
* 获取属性配置文件参数值
* @param key 参数名称
* @param def 参数默认值
* @return 参数值
*/
public static int get(String key, int def){
try{
def = Integer.parseInt(getInit().getProperty(key));
}catch(Exception e){
e.printStackTrace();
return def;
}
return def;
}
public static long get(String key, long def){
try {
def = Long.parseLong(getInit().getProperty(key));
} catch (Exception e) {
e.printStackTrace();
return def;
}
return def;
}
/**
* 获取属性配置文件参数值
* @param key 参数名称
* @param def 参数默认值
* @return 参数值
*/
public static String getUtil(String key, String def){
String val = getUtil().getProperty(key);
if(val == null || val.length() == 0){
return def;
}
return val;
}
public static long getUtil(String key, long def){
long val = Long.parseLong(getUtil().getProperty(key));
if(val == 0){
return def;
}
return val;
}
/**
* 获取属性配置文件参数值
* @param key 参数名称
* @param def 参数默认值
* @return 参数值
*/
public static String getChidProperty(String key,String def){
String val = getChid().getProperty(key);
if(val == null || val.length() == 0){
return def;
}
return val;
}import com.jinlou.util.PropertiesUtil;
public class Test {
public static void main(String[] args) {
//从配置文件中去key=test的value 如果配置文件中无此key则返回默认值
String test = PropertiesUtil.get("test","默认值");
System.out.println(test);
}
}