【发布时间】:2015-09-28 20:39:51
【问题描述】:
我有一些要求,我想在我正在使用我的 spring 应用程序的属性文件中写入/更新值。
我已经用谷歌搜索了它,但我没有找到使用 Spring 的直接方法。
有没有人知道怎么做,或者有没有最好的方法。
提前致谢。
【问题讨论】:
标签: java spring properties-file
我有一些要求,我想在我正在使用我的 spring 应用程序的属性文件中写入/更新值。
我已经用谷歌搜索了它,但我没有找到使用 Spring 的直接方法。
有没有人知道怎么做,或者有没有最好的方法。
提前致谢。
【问题讨论】:
标签: java spring properties-file
你可以这样实现:
public void saveParamChanges() {
try {
// create and set properties into properties object
Properties props = new Properties();
props.setProperty("Prop1", "toto");
props.setProperty("Prop2", "test");
props.setProperty("Prop3", "tata");
// get or create the file
File f = new File("app-properties.properties");
OutputStream out = new FileOutputStream( f );
// write into it
DefaultPropertiesPersister p = new DefaultPropertiesPersister();
p.store(props, out, "Header COmment");
} catch (Exception e ) {
e.printStackTrace();
}
}
编辑:使用 org.springframework.Util 中的 defaultPropertiesPersiter 更新
【讨论】: