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

虾米哥 阅读:257 2021-03-31 12:44:31 评论:0

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); 
                        } 
                    } 
                } 
            }

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们