0

I have a method that sets a few properties using Java Properties if the config file does not exists in the local drive. However the bad thing I found out was even if I change a few properties in my code, the method just checks if the file exists and does not update the file with new properties.

The other condition is the user might have overwritten one of the property in the config file. So the method that sets the property should not overwrite the values on the config file with my code.

Here is what I did

    private void setDefaultConfig() {
    try {
        if (!checkIfFileExists(configFile)) {
            setProperty(configFile, "cfgFile", "cfg.xml");
            setProperty(configFile, "type", "emp");
            setProperty(configFile, "url", "www.google.com");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

setPropertyMethod just sets the property on the specified file.

Now if I add another property in my method the user won't get the new property because I'm just checking if the file exists.

For Eg : If the user changes the "url" property to have "www.yahoo.com" then my code in the setDefaultConfig method should not replace the value with "www.google.com"

Is there any method that can handle this situation?

    protected void setProperty(String fileName, String property, String value) {
    Properties prop = new Properties();
    FileOutputStream fileOut = null;
    FileInputStream fileIn = null;
    try {
        File file = new File(fileName);
        if(checkIfFileExists(fileName)) {
            fileIn = new FileInputStream(file);
            prop.load(fileIn);
        }
        prop.setProperty(property, value);
        fileOut = new FileOutputStream(fileName);
        prop.store(fileOut, null);
    } catch (IOException io) {
        io.printStackTrace();
    } finally {
        if (fileOut != null) {
            try {
                fileOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Damien-Amen
  • 7,232
  • 12
  • 46
  • 75
  • Can we see the code of `setProperty` and a sample `configFile`? –  Feb 10 '15 at 16:51
  • You actually have to write the `Properties` object you retrieved back to the file you got it from to persist changes. – Ceiling Gecko Feb 10 '15 at 16:53
  • This may help: http://stackoverflow.com/questions/7461901/how-to-overwrite-one-property-in-properties-without-overwriting-the-whole-file – Anto Feb 10 '15 at 16:56

0 Answers0