当前位置 : 主页 > 编程语言 > java >

idea 读取resource目录下的文件

来源:互联网 收集:自由互联 发布时间:2021-06-28
utils String path = RunLog4j.class.getClassLoader().getResource("log4j.properties").getPath(); System.out.println(path); //输出/D:/D/idea_test_workplace/log4JDemo/target/classes/log4j.properties 文件的绝对路径 public class LogUtil
utils
String path = RunLog4j.class.getClassLoader().getResource("log4j.properties").getPath();
 System.out.println(path);
 //输出/D:/D/idea_test_workplace/log4JDemo/target/classes/log4j.properties 文件的绝对路径
  public class LogUtil {
    public static void init(){
        Properties props = new Properties();
        try {
            FileInputStream istream = new FileInputStream(path);
            props.load(istream);
            istream.close();
            PropertyConfigurator.configure(props);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
    }
}
使用
public class RunLog4j {
    //初始化log4j文件的配置
    static {
        LogUtil.init();
    }
    private static final Log logger = LogFactory.getLog(RunLog4j.class);
    public static void main(String[] args) {
    //        logger.info("This is info message.");
//        logger.debug("This is debug message.");
//
//
//        logger.error("This is error message.");
    
    }
网友评论