Java 读取jar内包资源文件和读取jar包外资源文件

Java 读取jar包内资源文件:

           # 读取jar 包内资源文件application.properties
           InputStream appPropertiesInputStream = ApplicationPropertiesHolder.class
                    .getClassLoader().getResourceAsStream("application.properties");

            if (appPropertiesInputStream != null) {
                try {
                    Properties appProperties = new Properties();
                    appProperties.load(appPropertiesInputStream);
                    if (logger.isDebugEnabled()) {
                        logger.debug("application.properties:");
                        for (Entry<Object, Object> entry : appProperties.entrySet()) {
                            logger.debug("*" + entry.getKey() + "=" + entry.getValue());
                        }
                    }
                    for (Entry<Object, Object> entry : appProperties.entrySet()) {
                        defaultProperties.put(entry.getKey(), entry.getValue());
                    }
                } catch (IOException e) {
                    logger.error("Load application.properties error", e);
                } finally {
                    if (appPropertiesInputStream != null) {
                        try {
                            appPropertiesInputStream.close();
                            appPropertiesInputStream = null;
                        } catch (IOException e) {
                            logger.error("InputStream Colse Error:", e);
                        }
                    }
                }
            }

Java 读取jar包外资源文件:

# 读取jar 同级目录的config/application.properties 文件 
String dir =new File(ApplicationPropertiesHolder.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getParent();
            InputStream appPropertiesInputStream = new FileInputStream(new File(dir+File.separator+"config/application.properties"));

            if (appPropertiesInputStream != null) {
                try {
                    Properties appProperties = new Properties();
                    appProperties.load(appPropertiesInputStream);
                    if (logger.isDebugEnabled()) {
                        logger.debug("application.properties:");
                        for (Entry<Object, Object> entry : appProperties.entrySet()) {
                            logger.debug("*" + entry.getKey() + "=" + entry.getValue());
                        }
                    }
                    for (Entry<Object, Object> entry : appProperties.entrySet()) {
                        defaultProperties.put(entry.getKey(), entry.getValue());
                    }
                } catch (IOException e) {
                    logger.error("Load application.properties error", e);
                } finally {
                    if (appPropertiesInputStream != null) {
                        try {
                            appPropertiesInputStream.close();
                            appPropertiesInputStream = null;
                        } catch (IOException e) {
                            logger.error("InputStream Colse Error:", e);
                        }
                    }
                }
            }

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值