16

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 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.

Thanks in advance.

mevada.yogesh
  • 1,118
  • 3
  • 12
  • 35

2 Answers2

22

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

Deh
  • 499
  • 5
  • 16
  • 4
    Thanks for the answer. But this is something I know. I want to do it through some method in spring only. – mevada.yogesh Jul 10 '15 at 08:22
  • @Yogesh is that what you mean by spring method? – Deh Jul 10 '15 at 08:38
  • 2
    @Yogesh, the Javadoc for [DefaultPropertiesPersister](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/DefaultPropertiesPersister.html) reads, "As of JDK 1.6, Properties.load/store will also be used for readers/writers, effectively turning this class into a plain backwards compatibility adapter", so even Spring isn't using a Spring-only method...it uses the JDK method. You should accept Deh's answer. – Paul Jul 27 '15 at 18:37
  • 2
    Isnt there a SPRING endemic approach from an annotation, like using @value keyword for reading the parameters from the property file? – hirosht Sep 04 '17 at 06:44
  • 1
    @hirosht I don't think there is. And this is a bummer as it seems you have to keep a separate file for manipulations with dynamic parameters. – Deniss M. Feb 09 '18 at 08:50
  • shouldn't the app-properties.properties file be saved in resource directory? – Sip Apr 18 '18 at 08:12
  • 1
    Is there a way to know where the properties were read from and write back over that? Also is there a way to write a class out, rather then specify props? – Brian S Oct 16 '20 at 18:26
0

Extending the existing answer by @deh, read and write using spring-boot. Note it is not using the typical classpath: in @PropertySource. To automate writing of more properties you could enumerate all fields with reflection.

@Data
@Configuration
@PropertySource("file:offset.properties")
public class Offset {

    @Value("${offset:0}")
    private int offset;

    public void save(int newOffset) {
        try {
            Properties props = new Properties();
            props.setProperty("offset", "" + newOffset);
            File f = new File("offset.properties");
            OutputStream out = new FileOutputStream( f );
            DefaultPropertiesPersister p = new DefaultPropertiesPersister();
            p.store(props, out, null);
        } catch (Exception e ) {
            e.printStackTrace();
        }
    }
}
stacker
  • 68,052
  • 28
  • 140
  • 210