在spring中写入/更新属性文件值

我有一些要求,我想在我使用my 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进行更新