静态方法中读取配置文件,拿小本本记下来(健忘体质 -.-!)
Properties props = new Properties(); InputStream in = MailUtil.class.getClassLoader().getResourceAsStream(fileName); if (in == null) {throw new RuntimeException("配置文件" + fileName + "不存在");}props.load(in);
20181031个人补充:
修炼心法,查阅API得知:
ClassLoader类加载器:将java文件加载到jvm中
获取类加载器:类名.class.getClassLoader()
类加载器的方法:
getResource(String name);//获取给定名称的资源,
getResource(String name).getPath();//获得资源的路径(类加载器是从classpath下开始加载类的)
getResourceAsStream(fileName);//获得指定资源的输入流