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
port=465
password=testpassword
host=smtp.mail.yahoo.com
username=testuser

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 email.properties in the folder where PropertiesDemo class is located.
  • loadEmailProperties – Reads the email.properties and loads the properties into a HashMap.
  • printPropertiesInConsole – Prints the key value pairs loaded from the email.properties in the console.

Java Example: Reading and Writing Properties File

import java.io.FileReader;
import java.io.FileWriter;
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{
        createEmailProperties();
        Map<String,String> props = loadEmailProperties();
        printPropertiesInConsole(props);
    }

    // Write a new properties file in Java
    private static void createEmailProperties() throws Exception {
        Properties p = new Properties();
        p.setProperty("host", "smtp.mail.yahoo.com");
        p.setProperty("port", "465");
        p.setProperty("username","testuser");
        p.setProperty("password", "testpassword");
        
        FileWriter writer = new FileWriter("email.properties");
        p.store(writer, "email server configuration");
        writer.close();
    }
    
    // Read a properties file in Java
    private static Map<String,String> loadEmailProperties() throws Exception {
        Map<String,String> propertyMap = new HashMap();
        
        FileReader reader = new FileReader("email.properties");
        Properties p = new Properties();
        p.load(reader);
                
        Enumeration keys = p.propertyNames();
        while(keys.hasMoreElements()) {
            String key = (String)keys.nextElement();
            propertyMap.put(key,p.getProperty(key));
        }
        
        reader.close();
        return propertyMap;
    }
    
    // Print all properties to the console
    private static void printPropertiesInConsole(Map<String,String> props) {
        for(String key:props.keySet()) {
            System.out.println(key+":"+props.get(key));
        }
    }
}

 

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("email.properties");
    Properties p = new Properties();
    p.load(stream);

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

    stream.close();
    return propertyMap;
}

Do you have a programming problem that you are unable to solve? Please send us your problem and we will publish the solution! Please email us.

Leave a Reply