Checking if properties file exists and has required properties

Aki K picture Aki K · Mar 30, 2014 · Viewed 10.7k times · Source

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.

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?

Summary of my requirements:

  1. Check if the file exists
  2. Check if it has the required properties
  3. Check if it has extra properties
  4. Create the file with the required properties if it doesn't exist or if there are extra or missing properties

Source files and Netbeans Project download

Source:

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() && 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
                    && 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();
            }
        }

    }
    }
}

Answer

Saša Zejnilović picture Saša Zejnilović · Nov 25, 2015

this is my way. (not sure if more elegant, but it can be an inspiration/different aproach)

Try/catch should be enough to see if the file exists or not

try {
  loading files etc...
    } catch (FileNotFoundException e) {
        throw new MojoExecutionException( "[ERROR] File not found", e );
    } catch (IOException e) {
        throw new MojoExecutionException( "[ERROR] Error reading properties", e );
}

Code that checks your loaded prop:

Properties tmp = new Properties();
for(String key : prop.stringPropertyNames()) {
  if(tmp.containsKey(key)){ 
    whatever you want to do...
  }
}

I use tmp, a new properties variable, to compare with ,but the key variable will hold a string so in the if statement you can compare it to array of strings and the way you do it is up to you.