我们在项目中可以把一些属性配置到×××.properties中,比如数据库连接信息。现在问题来了,我的属性文件中有一些是需要根据后台得到的数据来动态改变的,请问这个要怎么实现?java或者flex都行。
刚好最近也涉及到这里了,给你个方法吧:
[code="java"]private void editProperties(HashMap map){
String ret = "0";
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
//获取配置文件
InputStream in = classLoader.getResource("xxx.properties").openStream();
Properties props = new Properties();
props.load(in);
//重新装入配置文件信息
Iterator<Entry<String, String>> it = map.entrySet().iterator();
String key = "";
String value = "";
while(it.hasNext()){
Entry<String, String> e = it.next();
key = e.getKey();
value = e.getValue();
if(value.equals("")){continue;}
if(key.equals("要修改的参数")){
props.setProperty("参数名",值);
}else if(key.equals("xyz")){
//do something;
}else{
//do something;
}
}
in.close();
String path = classLoader.getResource("xxx.properties").getPath();
FileOutputStream fos = new FileOutputStream(path);
//重新保存配置文件信息
props.store(fos, "edit from jsp page time:"+new Date());
ret = "1";
}catch (Exception e) {
e.printStackTrace();
}
}[/code]