private static final String FILE_NAME = "test.properties";
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream( FILE_NAME ); 程序执行的时候,property文件的内容是不是都读到内存了?
这时候更改property文件的内容(不重起tomcat),再次读取的时候得到的值还是更改之前的内容。有简单的方法能让读到最新的文件内容么?(前提是不重起服务)
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream( FILE_NAME ); 程序执行的时候,property文件的内容是不是都读到内存了?
这时候更改property文件的内容(不重起tomcat),再次读取的时候得到的值还是更改之前的内容。有简单的方法能让读到最新的文件内容么?(前提是不重起服务)
參考一下別人的做法
private static final String FILE_NAME = "test.properties";
String path = Thread.currentThread().getContextClassLoader()
.getResource("").getPath();
path = path + FILE_NAME;
FileInputStream in = new FileInputStream(path);
properties.load(in);
in.close();
这样可以了。
Following is a simple one.
-----------------------------------------------
Map<String, String> prop = new HashMap<String, String>();
String parameterSeparator = "=";
BufferedReader br = getBufferedReader(propName);
StringBuffer sb = new StringBuffer();
int j = 0;
for (String line = br.readLine(); line != null; line = br.readLine()) {
j++;
if ("".equals(line) || "#".equals(line.substring(0, 1)))
continue;
if (line.indexOf(parameterSeparator) == -1) {
continue;
}
String _key = trim(line.substring(0, line.indexOf(parameterSeparator)));
String _value = trim(line.substring(line.indexOf(parameterSeparator) + 1)); prop.put(_key, _value);
} private static BufferedReader getBufferedReader(String fileName) throws Exception {
return new BufferedReader(new InputStreamReader(getInputStream(fileName)));}public static InputStream getInputStream(String fileName)throws Exception{
return (InputStream)AccessController.doPrivileged(new PrivilegedExceptionAction() { public Object run() {
InputStream is = null; try {
is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fFileName);
if (null == is) {
is = new FileInputStream(fFileName);
}
} catch (Exception e) { e.printStackTrace();
}
return is;
}
}
);
}