Easiest Way to 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

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

4 Different Ways to Iterate Through a Map in Java

Leave a Reply