jar包里面的代码如何读取jar包中的配置文件?
您好,读取jar包内配置文件:
InputStream in = this.getClass().getClassLoader().getResourceAsStream("/configfilename.properties");
读取jar包外配置文件:
String filePath = System.getProperty("user.dir") + "/conf/configfilename.properties";
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
另外,如果app中使用到log4j.properties文件,默认的存放路径是src/log4j.properties,同上面一样,我想把log4j.properties放在其他目录中,这样一来,在修改log4j配置文件的时候无需重新打jar包。
在main函数第一行添加如下代码:
PropertyConfigurator.configure(System.getProperty("user.dir") + "/conf/log4j.properties");
Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有