Write/Update spring 中的属性文件值

Write/Update properties file value in spring

我有一些要求 write/update 我正在使用 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();
   }
}

source

编辑:使用 org.springframework.Util

中的 defaultPropertiesPersiter 更新