title: Setting Java properties without overriding user written values
body: <p>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. </p>
  
  <p>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.</p>
  
  <p>Here is what I did </p>
  
  <pre><code>    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();
      }
  }
  </code></pre>
  
  <p>setPropertyMethod just sets the property on the specified file.</p>
  
  <p>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. </p>
  
  <p>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"</p>
  
  <p>Is there any method that can handle this situation?</p>
  
  <pre><code>    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();
              }
          }
      }
  }
  </code></pre>