title: Checking if properties file exists and has required properties
body: <p>I have written the code bellow to check if a properties file exists and has the required properties. If it exists it prints the message that the file exists and is intact, if not then it creates the properties file with the required properties.</p>
  
  <p>What I wanted to know is, is there a more elegant way of doing this or is my way pretty much the best way? Also the minor problem that I'm having is that with this way it doesn't check for extra properties that should not be there, is there a way to do that?</p>
  
  <p>Summary of my requirements:</p>
  
  <ol>
  <li>Check if the file exists</li>
  <li>Check if it has the required properties</li>
  <li>Check if it has extra properties</li>
  <li>Create the file with the required properties if it doesn't exist or if there are extra or missing properties</li>
  </ol>
  
  <p><a href="https://mega.co.nz/#!GFoQRK4B!IFAFdn_wVuMCvYz8LKoXyaSPOimC72dhc8jikVbS24o" rel="nofollow">Source files and Netbeans Project download</a></p>
  
  <p><strong>Source:</strong></p>
  
  <pre><code>import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.util.Properties;
  
  public class TestClass {
  
      public static void main(String[] args) {
  
          File propertiesFile = new File("config.properties");
          if (propertiesFile.exists() &amp;&amp; propertiesExist(propertiesFile)) {
              System.out.println("Properties file was found and is intact");
          } else {
              System.out.println("Properties file is being created");
              createProperties(propertiesFile);
              System.out.println("Properties was created!");
          }
      }
  
      public static boolean propertiesExist(File propertiesFile) {
          Properties prop = new Properties();
          InputStream input = null;
          boolean exists = false;
  
          try {
              input = new FileInputStream(propertiesFile);
  
              prop.load(input);
  
              exists = prop.getProperty("user") != null
                      &amp;&amp; prop.getProperty("pass") != null;
  
          } catch (IOException ex) {
              ex.printStackTrace();
          } finally {
              if (input != null) {
                  try {
                      input.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }
  
          return exists;
      }
  
      public static void createProperties(File propertiesFile)
      {
          Properties prop = new Properties();
      OutputStream output = null;
  
      try {
  
          output = new FileOutputStream(propertiesFile);
  
          prop.setProperty("user", "username");
          prop.setProperty("pass", "password");
  
          // save properties to project root folder
          prop.store(output, null);
  
      } catch (IOException io) {
          io.printStackTrace();
      } finally {
          if (output != null) {
              try {
                  output.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
  
      }
      }
  }
  </code></pre>