-
配置文件放在src的目录下面:
这是我们需要读取的就是jdbc2.properties配置文件信息
-
@WebServlet("/servletProperties4") public class ServletProperties4 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("servletproeprties4执行了"); InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc2.properties"); Properties prop = new Properties(); prop.load(inputStream); prop.list(System.out); //关闭流 inputStream.close(); }
这时候就可以读取到配置配置文件里面的信息了!
如果不用类加载器需要获取配置文件的需要在配置文件前面加"/"
//InputStream resource = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
InputStream resource = JDBCUtils.class.getResourceAsStream("/druid.properties");
pro.load(resource);
不用类加载器的时候读取配置文件需要在getResourceAsStream("/配置文件名字.properties")