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