A Java Properties file method

In an earlier Java Properties file example I showed how to load a Java Properties file and then access the elements in the Properties object, but for today, if you just need a method to read a Java Properties file, I hope the following source code will be helpful to you:

/**
 * Read a Java properties file and return it as a Properties object.
 */
public static Properties readPropertiesFile(String filename)
throws IOException {
    Properties properties = new Properties();
    properties.load(new FileInputStream(filename));
    return properties;
}

As you can see, it assumes the filename you gave it is a Java Properties file, and it reads the file contents (“loads” the file contents) into a Java Properties object, then returns that Properties object. Of course things can go wrong any time you try to access a file on a filesystem, so this method throws an IOException when things go wrong.