Write/Update properties file value in spring

前端 未结 1 784
执笔经年
执笔经年 2020-12-16 16:50

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

I have googled it but I have not found a

相关标签:
1条回答
  • 2020-12-16 17:46

    You can achieve that like this :

    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

    EDIT : updated with the defaultPropertiesPersiter from org.springframework.Util

    0 讨论(0)
提交回复
热议问题