項目中的一些配置文件,如dbconfig.properties log4j.xml 不想打包進jar。
因爲可能會修改其中的一些配置信息,打包進jar,就變得比較笨拙,不方便修改文件。試了幾種方法最後總結出以下的幾種方式
,實現在jar包中讀取外部配置文件。
方法一:
關鍵代碼。
讀取properties文件方法:
InputStream ins = getClass().getResourceAsStream("/resource/dbconfig.properties");
但是log4j.xml 又需要單獨處理:
PropertyConfigurator.configure(System.getProperty("user.dir") + "/resource/log4j.xml");
方法二:
配置文件out.properties和jiar包在同一個目錄下面:
FileInputStream inputStream1 = new FileInputStream("out.properties");
配置文件in.properties在jar包內部:
InputStream inputStream2 = a.class.getResourceAsStream("/in.properties");