title: How do I update a properties file dynamically?
body: <p>My application is a batch process that pulls environment variables from an application.properties file. The password that I use must be updated every few months. I would like to automate the password update process and save the updated password in the properties file so it can be used in future runs, but any updates I try to make are not propagated to the application.config file.</p>
  
  <p>When I run in Intellij, there are no errors, but the file is not updated. When I run the application as a jar, it gives:</p>
  
  <pre><code>java.io.FileNotFoundException: file:{localpath}\application.jar!\BOOT-INF\classes!\application.properties (The filename, directory name, or volume label syntax is incorrect)
  </code></pre>
  
  <p>How do I dynamically update the application.properties file within the jar or do I need to create a new properties file outside of the jar when the application first runs??</p>
  
  <p>EXAMPLE CODE SNIPPETS BELOW:</p>
  
  <p>properties.config:</p>
  
  <pre><code>username=user
  password=password123
  </code></pre>
  
  <p>Application.java:</p>
  
  <pre><code>    //get properties
      prop = new Properties();
      InputStream input = null;
      try {
          input = new FileInputStream(f);
          // load a properties file
          prop.load(input);
      } catch (IOException ex) {
          logger.error("Error reading application.properties", ex);
          ex.printStackTrace();
      } finally {
          if (input != null) {
              try {
                  input.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  
      //update properties
      prop.setProperty("TESTSTRING", "testvalue");
  
      //write to properties file
      OutputStream os = null;
      try{
          File f = new File(Application.class.getClassLoader().getResource("application.properties").getFile());
          os = new FileOutputStream(f);
          prop.store(os, comments);
      }catch(IOException ex){
          logger.error("Error updating application.properties", ex);
          ex.printStackTrace();
      } finally {
          if(os != null){
              try{
                  os.close();
              } catch (IOException e){
                  e.printStackTrace();
              }
          }
  </code></pre>