第一次用getProperty()读取配置文件后,配置文件会被缓存到内存中,当我用setProperty()修改配置文件后,再去取值,系统会去缓存中读取,取回来的还是没修改前的信息,所以请教:
(1)配置文件是通过什么方式缓存到内存中的,静态变量?
(2)如何实现修改配置文件后,立刻在内存中重新加载配置文件?或者去掉缓存中的信息
(1)配置文件是通过什么方式缓存到内存中的,静态变量?
(2)如何实现修改配置文件后,立刻在内存中重新加载配置文件?或者去掉缓存中的信息
public String getPath(String filename, String strPath) throws IOException {
InputStream inputStream = this.getClass().getClassLoader()
.getResourceAsStream(filename);
String savePath = "";
Properties p = new Properties();
try {
p.load(inputStream);
savePath = p.getProperty(strPath);
} catch (IOException e1) {
e1.printStackTrace();
}finally{
inputStream.close();
}
return savePath;
}其他地方都是直接调的这个接口来读取配置文件
InputStream inputStream = null;
FileOutputStream outputStream = null;
Properties p = new Properties();
try {
inputStream = this.getClass().getClassLoader().getResourceAsStream(filename);
outputStream = new FileOutputStream(this.getClass().getClassLoader().getResource("/").getPath() + filename);
p.load(inputStream);
p.setProperty(propertyName, value).toString();
p.store(outputStream, "Description");
String reload = new Common().getPath(filename, propertyName);
} catch (FileNotFoundException e2) {
e2.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
inputStream.close();
outputStream.close();
}
}
1>刷新下项目-主要要刷新下项目下的配置文件(如果在MyEcplise下看不到你的那个配置文件就没必要刷新了)
2>load