在web工程中,程序无非两种,一种是servlet程序,一种是非servlet程序,servlet程序用
ServletContext去读,非servlet用
类装载器去读。
如:
//servlet程序中,用
ServletContext(db.properties为资源文件)
InputStream in = this.
getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties props = new Properties();
props.load(in);
Properties props = new Properties();
props.load(in);
//
非servlet程序中,通过类装载器去读取,但是
无法获取更新后的数据,因为类装载器只装载一次,服务器停止前不更新
FileInputStream in = newFileInputStream(path);Properties prop = new Properties();prop.load(in);prop.getProperty("url") //获取 db.properties中的url信息
// 非servlet程序中,通过类装载器去读取, 可以获取更新后的数据String path = userdao.class.getClassLoader(). getResource("db.properties"). getPath();
Properties prop = new Properties();prop.load(in);prop.getProperty("url")