How to Load Properties File in Java

Properties file is text file commonly used to store configuration data in Java programs. Each line in the properties file represents a property with name and value separated by an equal sign. Usually properties files are saved with the extension .properties.  We will creating and loading the sample properties file given below. It contains sample configuration for an email server,

#email server configuration
#Mon Feb 27 16:08:12 IST 2017

The above properties file is created using the following Java program. The first comment in the file is inserted by our Java program. The second comment is a timestamp and it indicates when the file was created. This timestamp comment is automatically added by the Properties class. It cannot be suppressed.

Following are the main functions in the sample program,

  • createEmailProperties - This creates a new properties file named in the folder where PropertiesDemo class is located.
  • loadEmailProperties - Reads the and loads the properties into a HashMap.
  • printPropertiesInConsole - Prints the key value pairs loaded from the in the console.

Java Example: Reading and Writing Properties File

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

// Java program to read and write properties file
public class PropertiesDemo {
    public static void main(String[] args) throws Exception{
        Map<String,String> props = loadEmailProperties();

    // Write a new properties file in Java
    private static void createEmailProperties() throws Exception {
        Properties p = new Properties();
        p.setProperty("host", "");
        p.setProperty("port", "465");
        p.setProperty("password", "testpassword");
        FileWriter writer = new FileWriter("");, "email server configuration");
    // Read a properties file in Java
    private static Map<String,String> loadEmailProperties() throws Exception {
        Map<String,String> propertyMap = new HashMap();
        FileReader reader = new FileReader("");
        Properties p = new Properties();
        Enumeration keys = p.propertyNames();
        while(keys.hasMoreElements()) {
            String key = (String)keys.nextElement();
        return propertyMap;
    // Print all properties to the console
    private static void printPropertiesInConsole(Map<String,String> props) {
        for(String key:props.keySet()) {


How to Read Properties File from Classpath

If you want to read properties file from the classpath, use the following function. This requirement is common in web applications.

private static Map loadFromClasspath() throws Exception {
    Map<String, String> propertyMap = new HashMap();
    InputStream stream = PropertiesDemo.class.getResourceAsStream("");
    Properties p = new Properties();

    Enumeration keys = p.propertyNames();
    while (keys.hasMoreElements()) {
        String key = (String) keys.nextElement();
        propertyMap.put(key, p.getProperty(key));

    return propertyMap;