Read Properties File in Java With ResourceBundle

There are a number of ways to load and read properties file from Java, but the easiest and most straightforward is using the ResourceBundle class.

First, you need to create a properties file under resources folder. In a typical Maven project, this looks like the following

java resourcebundle properties file

 In this example, the properties file is called config.properties

The content of the properties file is in the format of name=value

Example:

browser=chrome

In a Java class, we can use the ResourceBundle class to read from the properties file:

public class ReadPropertiesFile {

    private static ResourceBundle rb = ResourceBundle.getBundle("config");

    public static void main(String[] args) {
        String browser = rb.getString("browser");
        System.out.println(browser);
    }
}

Output:

chrome