今天在做项目的时候,碰到了一个需求,需要动态的修改config.properties,原本是一个简单的问题,结果每次修改完之后,项目都会重新部署,这样的话,session就会自己关掉。其他的一些功能也无从谈起了。开发工具用的是eclipse。
操作properties的类如下:
public class ReadProperties {
private Map<String, String> props = new HashMap<String, String>();
private static String cfgFilePath = "/config.properties";
private String ZYTZ;
public String getZYTZ() {
return ZYTZ;
}
public void setZYTZ(String zytz) {
ZYTZ = zytz;
}
private void readConf(){
Properties properties = new Properties();
try {
properties.load(this.getClass().getResourceAsStream(
cfgFilePath));
} catch (Exception e) {
System.out.println("不能读取属性文件. " + "请确保" + cfgFilePath
+ "在CLASSPATH指定的路径中");
return;
}
Enumeration<Object> e = properties.keys();
//枚举所有 key-value对
while (e.hasMoreElements()) {
String keyO = (String) e.nextElement();
String value = properties.getProperty(keyO);
props.put(keyO, value);
}
}
public void writeConf(String key,String value){
Properties properties = new Properties();
try {
properties.load(this.getClass().getResourceAsStream(
cfgFilePath));
OutputStream fos = new FileOutputStream(this.getClass().getResource(cfgFilePath).getPath());
//修改一个
properties.setProperty(key, value);
properties.store(fos,"this is header");
fos.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public String getStrByParam(String param){
if(props.isEmpty())
readConf();
String paramValue = props.get(param);
return paramValue;
}
}
?请问有什么方法能够动态修改properties之后,不自动部署项目吗?