1.读取项目src目录下文件
Properties p = new Properties();
p.load(this.getClass().getClassLoader().getResourceAsStream("jdbc.properties"));
String name = p.getProperty("name");
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"));
Integer id = Integer.parseInt(p.getProperty("id"));
备注:测试发现第一种方式修改属性文件后没有进行热部署自动编译,建议采用第二种方式。