工具类3:ConfigUtil

public class ConfigUtil {

private static PropertiesConfiguration config=null;
static{
try{
//config = new PropertiesConfiguration("app.properties");
//config.setEncoding("UTF-8");
//config.setHeader(header)
config = new PropertiesConfiguration();
config.setEncoding("UTF-8");
config.load("application.properties");

}catch(Exception ex){
}
}


public static int getIntValue(String key){
int reInt = 1;
try{
//PropertiesConfiguration config = new PropertiesConfiguration("conf.properties");
reInt = config.getInt(key);
}catch(Exception ex){
ex.fillInStackTrace();
reInt = 0;
}
return reInt;
}

public static Long getLongValue(String key) {
Long reLong = 1l;
try{
//PropertiesConfiguration config = new PropertiesConfiguration("conf.properties");
reLong = config.getLong(key);
}catch(Exception ex){
ex.fillInStackTrace();
reLong = 0l;
}
return reLong;
}



public static double getDoubleValue(String key){
double reDouble = 1.0;
try{
//PropertiesConfiguration config = new PropertiesConfiguration("conf.properties");
reDouble = config.getDouble(key);
}catch(Exception ex){
ex.fillInStackTrace();
reDouble =1.0;
}
return reDouble;
}

public static String getStringValue(String key){
String str = "";
try{
//PropertiesConfiguration config = new PropertiesConfiguration("conf.properties");
str = config.getString(key);
}catch(Exception ex){
ex.fillInStackTrace();
str = "";
}
return str;
}

public static Boolean getBooleanValue(String key) {
Boolean flag = false;
try{
flag = config.getBoolean(key);
}catch(Exception ex){
ex.fillInStackTrace();
}
return flag;
}

public static void save(String key,Object o){
config.setProperty(key, o);
try{
//config.save("score.properties");
config.save("application.properties");
//config = new PropertiesConfiguration("app.properties");
config = new PropertiesConfiguration();
config.setEncoding("UTF-8");
config.load("application.properties");
}catch(Exception ex){
ex.printStackTrace();
}
}

public static void main(String[] args){
System.out.println("ewew");
System.out.println(getIntValue("vip.money"));
//ConfigUtil.save("rmb.money", 20);
//ConfigUtil.save("month.live", "123334");
System.out.println(ConfigUtil.getStringValue("month.live1"));
//System.out.println(getStringValue("month.live"));
}

// 读取配置文件
private static ResourceBundle cache = null;

static {
try {
cache = ResourceBundle.getBundle("application");
} catch (RuntimeException e) {
e.printStackTrace();
}
}

/**
* 功能描述:获取配置文件参数值
*
* @param str(参数KEY值)
* @return
*/
public static String getValue(String str) {
String s = cache.getString(str);
try {
s = new String(s.getBytes("ISO8859-1"), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return s;
}

/**
* 功能描述:获取指定配置文件参数的值
*
* @param strPropertiesFile(配置文件名称)
* @param strItem(参数名称)
* @return
*/
public String getPropertiesValue(String strPropertiesFile, String strItem) {
String strItemValue = "";
ResourceBundle resources1 = null;
try {
resources1 = ResourceBundle.getBundle(strPropertiesFile);
strItemValue = resources1.getString(strItem);
} catch (MissingResourceException e) {
System.out.println("ConfigInfo.getPropertiesValue error:"
+ e.getMessage());
}
return strItemValue;
}



}
转自:http://lucien-zzy.iteye.com/blog/2009495
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值