后台读取配置文件

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PropertieUtils {

    private final static Logger log = LoggerFactory.getLogger(PropertieUtils.class);
    
    private Properties    properties = new Properties();
    
    public PropertieUtils(){
        this.initProperties();
    }
    
    private void initProperties(){
        InputStream rs = null;
        String appPath = "";
        try{
            //如果jar包外有配置文件就以jar外的配置为准,否则读取jar包内的配置文件
            appPath = System.getProperty("user.dir");
            //appPath = CommUtils.getAppPath();
            File file = new File(appPath + File.separator + "xetl-message.properties");
            if (file.exists()){
                rs = new FileInputStream(file);
            }else{
                rs = Thread.currentThread().getContextClassLoader().getResourceAsStream("xetl-message.properties");
            }
            properties.load(rs);
        
        }catch(Exception e){
            log.error("读取xetl-message.properties配置文件失败," + appPath + "," + e.getMessage(), e);
            //throw new RuntimeException("读取xetl-message.properties配置文件失败" + e.getMessage() + "," + appPath);
        }finally{
            try{
                if (rs != null){
                    rs.close();
                }
            }catch(Exception e){
                
            }
        }
    }
    

    public String getConfigValue(String key)
    {
        return properties.getProperty(key);
    }

    public int getIntValue(String key)
    {
        String value = getConfigValue(key);
        return Integer.parseInt(value);
    }

    public String getDefaultValue(String key, String value)
    {
        String result = properties.getProperty(key);
        if (result == null || result.length() == 0)
        {
            return value;
        }
        return result;
    }
    

}
  main (){

PropertieUtils properties = new PropertieUtils("xetl-sun.properties");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值