我有一些要求,我想在我正在使用我的 spring 应用程序的属性文件中写入/更新值。
我已经用谷歌搜索过了,但我没有找到使用 Spring 的直接方法。
有没有人知道怎么做,或者有没有最好的方法。
提前致谢。
最佳答案
你可以这样实现:
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 更新
关于java - 在 Spring 写入/更新属性文件值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31334977/