獲取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");

測試獲取jar包同級目錄文件如圖:

在jar包中某個類定義下圖語句:

測試結果ok!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章