WEB应用中普通Java程序怎么读取资源文件
WEB应用中普通Java程序如何读取资源文件
Properties dbconfig = new Properties();
只会被类加载器加载一次:
//以下代码虽然可以读取资源文件的数据,但是无法获取更新后的数据
InputStream in = this.class.getClassLoader().getResourceAsStream("db.properties");
dbconfig .load(in);
每次访问都会加载:
//通过类装载的方式得到资源文件的位置,再通过传统方式读取资源文件的数据,这样可以读取到更新后的数据
String path = this.class.getClassLoader().getResource("db.properties").getPath();
FileInputStream in = new FileInputStream(path);
dbconfig .load(in);