59

Content of First.properties:

name=elango
country=india
phone=12345

I want change country from india to america. This is my code:

import java.io.*;
public class UpdateProperty 
{
    public static void main(String args[]) throws Exception 
    {   
        FileOutputStream out = new FileOutputStream("First.properties");
        FileInputStream in = new FileInputStream("First.properties");
        Properties props = new Properties();
        props.load(in);
        in.close();
        props.setProperty("country", "america");
        props.store(out, null);
        out.close();
    } 
}

Output content of First.properties:

country=america

The other properties are deleted. I want update a particular property value, without deleting the other properties.

ADTC
  • 8,999
  • 5
  • 68
  • 93
Elangovan
  • 611
  • 1
  • 6
  • 5
  • Similar: https://stackoverflow.com/questions/7461901/how-to-overwrite-one-property-in-properties-without-overwriting-the-whole-file and https://stackoverflow.com/questions/565932/a-better-class-to-update-property-files – Vadzim Jun 30 '17 at 09:44

3 Answers3

105

Open the output stream and store properties after you have closed the input stream.

try (FileInputStream in = new FileInputStream("First.properties")) {
    Properties props = new Properties();
    props.load(in);
}
        
try (FileOutputStream out = new FileOutputStream("First.properties"))
    props.setProperty("country", "america");
    props.store(out, null);
}
Born2Smile
  • 2,918
  • 1
  • 18
  • 20
Vasyl Keretsman
  • 2,688
  • 2
  • 18
  • 15
34

You can use Apache Commons Configuration library. The best part of this is, it won't even mess up the properties file and keeps it intact (even comments).

Javadoc

PropertiesConfiguration conf = new PropertiesConfiguration("propFile.properties");
conf.setProperty("key", "value");
conf.save();    
AnirbanDebnath
  • 990
  • 16
  • 26
  • 6
    This is much better answer than the accepted one – Joe Hackerberg Feb 16 '17 at 14:19
  • @José Mª I downloaded https://commons.apache.org/proper/commons-configuration/index.html ApacheCommons Configurator 2.1.1 and it seems that `save()` method and some others doesn't exists . Is there any alternative ? – GOXR3PLUS May 12 '17 at 07:47
  • @GOXR3PLUS Please check this website: https://commons.apache.org/proper/commons-configuration/userguide_v1.10/howto_properties.html There is a "Saving" section where its explained. – Joe Hackerberg May 12 '17 at 09:08
  • @José Mª Thanks for the link . It seems to be only for the version 1.10 , not for the version 2.1.1 which is the latest :) . I am searching inside https://commons.apache.org/proper/commons-configuration/userguide/user_guide.html to find something similar . – GOXR3PLUS May 12 '17 at 09:39
  • 1
    @GOXR3PLUS Sorry I can't test it right now, but I thought it should work because there is a annotation at the header of the website where you can see: "Last Published: 08 February 2017|Version: 2.1.1" It's quite confusing, but you can still use older versions. Unfortunately, I cannot help you any more with this :( – Joe Hackerberg May 12 '17 at 10:42
  • 1
    @JoséMª I had used the 1.1 version. Also, the 2.1 version has the `save()` method in it. https://commons.apache.org/proper/commons-configuration/apidocs/org/apache/commons/configuration2/PropertiesConfiguration.html – AnirbanDebnath May 22 '17 at 07:55
  • @AnirbanDebnath Thank you for the info, but how can that link demonstrate it? There are only 1 match with "`save()`" in the whole page and doesn't correspond to the method description. Where is `save()` hidden? – Joe Hackerberg May 22 '17 at 08:10
  • Okay you are using the apache-commons thats causing the issue. Use the commons-configuration one. Use this: https://mvnrepository.com/artifact/commons-configuration/commons-configuration/1.9 – AnirbanDebnath May 22 '17 at 09:23
  • I am using commons configuration2 but getting an error when using above code `PropertiesConfiguration() in PropertiesConfiguration can not be applied to (java.lang.String) – J. Doem Aug 20 '17 at 00:10
  • 1
    Hi @J.Doem: You can use the Older `1.10 version` which has a String args constructor to pass the file name directly. Else, for V2, you can use the `PropertyConfiguration` class's `loadIncludeFile(String fileName)` method to pass the File name. – AnirbanDebnath Aug 20 '17 at 08:22
  • 1
    This solution solved my 2 issues. 1. Saving the file without losing other data and also 2. Keys positions are not swapped (By using other solutions i.e., with FileInput & Output streams, positions of keys are getting swapped to random positions and also comments are getting removed). – Lakshmipathi G Mar 22 '22 at 13:10
10
Properties prop = new Properties();
prop.load(...); // FileInputStream 
prop.setProperty("key", "value");
prop.store(...); // FileOutputStream 
Joe2013
  • 1,007
  • 1
  • 9
  • 24