1.读取项目src目录下文件
- Properties p = new Properties();
- p.load(this.getClass().getClassLoader().getResourceAsStream("jdbc.properties"));
2.读取项目WEB-INF目录下文件
- String pro = this.getClass().getResource("/").getPath().split("WEB-INF")[0]+ "WEB-INF/jdbc.properties";
- FileInputStream fs = new FileInputStream(pro);
- Properties p = new Properties();
- p.load(new InputStreamReader(fs, "utf-8"));
本文介绍了如何在Java项目中从src和WEB-INF目录读取配置文件的方法。通过使用不同类加载器和文件路径技巧,可以灵活地定位并加载项目内的属性文件。
2298

被折叠的 条评论
为什么被折叠?



