1、打入jar
maven 項目默認打入jar的文件夾是src/main/resources 中
1、log4j配置文件直接放入就可以直接用
2、其他配置文件獲取方式
public static void main( String[] args )
{
InputStream in = App.class.getResourceAsStream("/config.properties");
// "/" 表示根目錄 ,這樣可以拿到該文件是因爲打完包後,文件目錄層級如下圖所示
// 或者 通過 App.class.getClassLoader().getResourceAsStream("config.properties"); 獲取
// ClassLoader就是從整個classes目錄找的,所以前面無需再加/。
//
InputStream in = App.class.getClassLoader().getResourceAsStream("config.properties");
Properties p = new Properties();
try {
p.load(in);
log.warn("我是告警信息");
System.out.println(p.get("collection"));
log.info("我是常規信息info");
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
log.error("woshicuwou -------------");
}
jar包中目錄層級
2、配置文件放到外面
1、log4j 加載配置文件
public class App {
static {
PropertyConfigurator.configure("log4j.properties");
}
public static void main(String[] args) {
...
}
2、其他配置文件也可以直接獲取 例如
public static void main( String[] args )
{
File f = new File("config.properties");
}