最近项目中需要用到动态读取properties文件,但是最后测试发现,每次修改键值之后,必须要重启服务器才会读取到最新修改的值,网上查了很多资料,也没人能完整解答,最后综合网上部分方案,终于发现问题所在,不用多解释,直接上代码,分享出来希望能帮助到遇到同样问题的程序猿们。
- package xxx.xxx.xxx.xxx;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.util.Properties;
- /**
- * <p>Description: properties文件操作工具</p>
- * @author cs
- */
- public class PropertiesUtil {
- private String paramsPath = "params.properties";
- private String counterPath = "counter.properties";
- /**
- * <p>Description: 根据key读取value</p>
- * @author cs
- * @version 2013-10-24
- * @modifier cs
- * @modifiDate 2013-10-24
- * <p>modifiContent: 首次创建</p>
- * @param isParamProper 是否是读取参数配置信息,true是,false则读取计数器配置
- * @param key
- * @return
- */
- public String readValue(boolean isParamProper, String key) {
- Properties props = new Properties();
- String path = Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(1);
- FileInputStream fis = null;
- try {
- fis = new FileInputStream(path+(isParamProper?paramsPath:counterPath));
- //不能用以下的方式,否则必须要重启服务器才能读取到最新的数据,问题就出在这
- // InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(isParamProper?paramsPath:counterPath);
- // props.load(in);
- props.load(fis);
- String value = props.getProperty(key);
- return value;
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if(null!=fis) {
- try {
- fis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return null;
- }
- /**
- * <p>Description: 写入properties信息</p>
- * @author cs
- * @version 2013-10-24
- * @modifier cs
- * @modifiDate 2013-10-24
- * <p>modifiContent: 首次创建</p>
- * @param isParamProper 是否是写入参数配置信息,true是,false则写入计数器配置
- * @param parameterName
- * @param parameterValue
- * @throws IOException
- */
- public void writeProperties(boolean isParamProper,String parameterName,
- String parameterValue) {
- Properties prop = new Properties();
- String path = Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(1);
- FileInputStream fis = null;
- OutputStream fos = null;
- try {
- // fis = Thread.currentThread().getContextClassLoader().getResourceAsStream(isParamProper?paramsPath:counterPath);
- // 从输入流中读取属性列表(键和元素对)
- fis = new FileInputStream(path+(isParamProper?paramsPath:counterPath));
- prop.load(fis);
- fis.close();//一定要在修改值之前关闭fis
- fos = new FileOutputStream(path+(isParamProper?paramsPath:counterPath));
- prop.setProperty(parameterName, parameterValue);
- prop.store(fos, null);//第二个参数为注释
- fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if(null!=fos) {
- try {
- fos.close();
- if(null!=fis) fis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
另外需要说明的是尽量别用静态的方式写读取和写入方法,每次调用时,重新new该对象更妥。
本示例中,properties文件默认放在classes目录下。
public String readValue(String key){
Properties props = new Properties();
String path = Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(1);
System.err.println(path);
FileInputStream fis = null;
try {
fis = new FileInputStream(path + "checkname.properties");
InputStreamReader sr = new InputStreamReader(fis,"UTF-8");
props.load(sr);
String value = props.getProperty(key);
return value;
} catch (Exception e) {
logger.error("error: " + e);
} finally {
if(null!=fis) {
try {
fis.close();
} catch (IOException e) {
logger.error("error: " + e);
}
}
}
return null;
}