0

Let say i have property file test.properties.

There are already defined some key/values pairs e.g:

key1=value1
key2=value2
key3=value3

I change in memory some value of these properties (let say only one key's value). I would like to store changes into property file, but to store really only changed key/value => not rewrite whole file.

Is that possible?

Any implementation of some library to I could achieve something like that?

To Kra
  • 3,344
  • 3
  • 38
  • 45

2 Answers2

0

Look at java.util.prefs.Preferences

EDIT:

This is a Java utility class that does what you seem to want -- store key/value pairs (only strings as keys) without having to (re)write an entire file of them to change one value. Java has implemented them with system-dependent backing so they're portable.

arcy
  • 12,845
  • 12
  • 58
  • 103
0
    String fileName = "C:\\test\\test.txt";
    File f = new File(fileName);
    InputStream is = new FileInputStream(f);
    Properties p = new Properties();
    p.load(is);
    p.setProperty("key3","value4");
    OutputStream os = new FileOutputStream(f);
    p.store(os,"comments");

But I think this will overwrite the entire properties file.

robin
  • 1,893
  • 1
  • 18
  • 38