Java Property Files: A Beginner’s Guide on how to create, load, and use Properties

What is a Property File?

A .properties file is a file used to store the configurable parameters of your Java application. The standard format for the Property File is one parameter per line with key value pairs delimited by the  equals (=) character. Comments are allowed in the file by placing a # or ! character at the beginning of the line. If by chance you would like to use multiple lines for your property value the back slash () character can be used to denote the value continues on the next line.

Sample File

#sample comment
user = MDBitz
password = test123
website = mdbitz.com

Loading the Properties File into your application

Properties can be loaded into your application in various methods the two most common is to load the properties from the classpath or from an external properties file. Loading the properties from the classpath means the file is bundled in with the java source code and a new deploy will be needed to modify the properties while external files can be changed more readily.

Loading from the Classpath

Loading from the classpath is performed by utilizing the ClassLoader to obtain the url of the desired property file. The Properties object then loads the input stream of the url that is returned by the openStream method.

Properties props = new Properties();
URL url = ClassLoader.getSystemResource(propsName);
props.load( url.openStream() );

Loading from an external file

To load an external file we first instantiate a new Properties object, then proceed to load the file by use of a FileInputStream. After loading the file we close the input stream to clean up our resource usage.

Properties props = new Properties();
FileInputStream fis = new FileInputStream(propsFile);
props.load( fis );
fis.close();

Using the Properties Class

Once the properties have been loaded they can be utilized by the getProperty method. This method takes a string representing the key and returns the property assigned to that key.  To verify that a property exists before trying to obtain it the containsKey method can be used to check if the Properties object cans a property with the inputted key.

if( props.containsKey( "website" ) ) {
    System.out.println( props.getProperty( "website" ) );
} else {
    System.out.println( "Default Website" );
}

Conclusion

The use of a Properties Files can easily add dynamic elements to your application on run time. They can be used to specify the database connections to be utilized or more general properties such as labels or logging levels.

Resources

PropertiesUtil Class

package com.mdbitz.utils;

/*
 * Import Statements
 */
import java.util.Properties;
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.net.URL;

/**
 * Utility for loading property file into Properties object
 * @author MDBitz - (Matthew Denton)
 */
public class PropertiesUtil {

	/**
	 * Private constructore to disallow instantiation
	 */
	private PropertiesUtil()
	{

	}

    /**
     * Load a properties file from the classpath
     * @param propsName
     * @return Properties
     * @throws Exception
     */
    public static Properties load(String propsName)
        throws Exception
    {
        Properties props = new Properties();
        URL url = ClassLoader.getSystemResource(propsName);
        props.load(url.openStream());
        return props;
    }

    /**
     * Load a Properties File
     * @param propsFile
     * @return Properties
     * @throws IOException
     */
    public static Properties load(File propsFile)
        throws IOException
    {
        Properties props = new Properties();
        FileInputStream fis = new FileInputStream(propsFile);
        props.load(fis);
        fis.close();
        return props;
    }

}
// Java //

Comments & Questions

Add Your Comment