运行jar包读取外部配置文件

测试路径:

	public static void main(String[] args) {
		String path1 = System.getProperty("user.home");//当前登录用户目录
		String path2 = System.getProperty("user.dir");//jar包所在目录名
		System.out.println(path1+"=="+path2);
	}

 使用列子:

 public static void main(String[] args) {
        try {
            PropContainer pro = PropContainer.getInstance(System
                    .getProperty("user.dir")
                    + File.separator
                    + "config.properties");
            InitExcelData.init(pro.getProperty("excelPath"));

            String sourcePath = pro.getProperty("sourcePath");
            String tempPath = pro.getProperty("tempPath");
            String destPath = pro.getProperty("destPath");
            String errorPath = pro.getProperty("errorPath");
            String bakPath = pro.getProperty("bakPath");
            String isBak = pro.getProperty("isBak");
       } catch (Exception e) {
            log.info(e.getMessage());
            e.printStackTrace();
        }

    }

 

public class PropContainer {
    private static final Logger log = Logger.getLogger(PropContainer.class);

    private Properties properties;

    private static String charset = "UTF-8";

    private static Map<String, PropContainer> map = new HashMap<String, PropContainer>();

    private PropContainer(String propertiesWay) {
        initConfig(propertiesWay);
    }

    public static PropContainer getInstance(String propertiesWay) {
        if (map.containsKey(propertiesWay)) {
            return map.get(propertiesWay);
        }
        PropContainer p = new PropContainer(propertiesWay);
        map.put(propertiesWay, p);
        return p;
    }

    private void initConfig(String propertiesWay) {
        properties = new Properties();
        try (InputStreamReader in = new InputStreamReader(new FileInputStream(
                propertiesWay), charset)) {
            properties.load(in);
        } catch (Exception e) {
            log.error("", e);
        }
    }

    public String getProperty(String key, String defaultValue) {
        if (properties == null) {
            return defaultValue;
        }
        return properties.getProperty(key, defaultValue);
    }
    
    public String getProperty(String key) throws RuntimeException{
        String tmp = properties.getProperty(key);
        if(StringUtils.isBlank(tmp)){
            throw new RuntimeException("属性为空!"+key);
        }
        return tmp;
    }

    public int getInt(String key, int defaultValue) {
        String prop = getProperty(key, "");
        if ("".equals(prop) || !NumberUtils.isDigits(prop)) {
            return defaultValue;
        }
        return Integer.parseInt(prop);
    }

    public boolean containsKey(String key) {
        if (properties != null && properties.containsKey(key)) {
            return true;
        }
        return false;
    }
}

 

转载于:https://www.cnblogs.com/judylucky/p/7047975.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值