HelloKoding

Practical coding guides

Read Properties File Example in Java

In this tutorial, you will learn to read / load properties file from classpath in Java by using load method of java.util.Properties class and getResourceAsStream method of ClassLoader class

Properties are configuration String values managed as key/value pairs. java.util.Properties is extended from java.util.Hashtable, provide the following features

  • loading key/value pairs into a Properties object from a stream
  • retrieve a value from its key
  • listing the keys and their values
  • testing to see if a particular key or value is in the Properties object
  • saving the properties to a stream

What you’ll need

  • JDK 8+
  • Maven 3+

Stack

  • Java
  • Properties file

Init project structure and dependencies

Project structure

├── src
│   └── main
│       ├── java
│       │   └── com
│       │       └── hellokoding
│       │           └── util
│       │               ├── ApplicationProperties.java
│       │               └── Main.java
│       └── resources
│           └── application.properties
└── pom.xml

Project dependencies

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hellokoding.util</groupId>
    <artifactId>properties-file-java</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <exec.mainClass>com.hellokoding.util.Main</exec.mainClass>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

How to read / load properties file

Given a properties file

application.properties

app.name=Hello Koding

The following give you an example on reading the properties file from classpath

ApplicationProperties.java

package com.hellokoding.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public enum ApplicationProperties {
    INSTANCE;

    private final Properties properties;

    ApplicationProperties() {
        properties = new Properties();
        try {
            properties.load(getClass().getClassLoader().getResourceAsStream("application.properties"));
        } catch (IOException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, e.getMessage(), e);
        }
    }

    public String getAppName() {
        return properties.getProperty("app.name");
    }
}
  • The load(InputStream inStream) method of Properties class reads a property list (key and element pairs) from the input byte stream.

    IOException, IllegalArgumentException and NullPointerException will be thrown if an error occurred when reading from the input stream, if the input stream contains malformed Unicode escape sequence and if the inStream is null respectively

  • The getResourceAsStream(String name) method of ClassLoader class returns an input stream for reading the specified classpath resource or returns null if the resource could not be found

Apart from getting the properties file from classpath, you can also load from other locations directly via FileInputStream or FileReader, for example

properties.load(new FileInputStream("src/main/resources/test.properties"));
properties.load(new FileReader("src/test.properties"));

Test

Main.java

package com.hellokoding.util;

public class Main {
    public static void main(String[] args) {
        System.out.println(ApplicationProperties.INSTANCE.getAppName());
    }
}

Run this command and check the result in the console log

mvn clean package exec:java

Source code

https://github.com/hellokoding/hellokoding-courses/blob/master/java-examples/java-properties-file

Follow HelloKoding