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
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
- How to create a file in Java
- How to write to a file in Java
- How to get current working directory in Java
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