Search code examples
javaproperties-file

How can I modify 1 key value pair out of 4 in a properties file?


I have 4 key value pairs in a properties file and I want to modify the value of 1 key. How can I do so without affecting the other key value pairs?


Solution

  • Here is a sample of loading a properties file, manipulating a value and storing again.

    try {
        File file = new File("YOUR PATH");
    
        FileInputStream input = new FileInputStream(file);
        Properties properties = new Properties();
        properties.load(input);
        input.close();
    
        properties.setProperty("YOUR_KEY", "YOUR_VALUE");
    
        FileOutputStream output = new FileOutputStream(file);
        properties.store(output, null);
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }