在spring中写入/更新属性文件值 [英] Write/Update properties file value in spring

查看:459
本文介绍了在spring中写入/更新属性文件值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些要求,我想在我使用my spring应用程序的属性文件中写入/更新值。

I have some requirement where I want to write/update the value in the properties file I am using the my spring application.

我用google搜索了但是我有没有找到使用Spring的直接方式。

I have googled it but I have not found a direct way of doing it using Spring.

是否有人知道如何做或有任何最好的方法。

Does any one aware of how to do it or is there any best way to do it.

提前致谢。

推荐答案

你可以这样做:

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进行更新

这篇关于在spring中写入/更新属性文件值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆