- Given a property file defining the properties of a java application.
- Property file will be residing in the resource folder/directory.
- Load the property file present in resource directory.
- Load property using Properties class in java.
- Properties class extends Hashtable. Hashtable will provide properties as key value pairs..
- We will iterate through key / value pairs of application’s properties.
- We will iterate through application’s properties & we will print all properties.
Program – read property file from resource directory in java (example)
package org.learn;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertyReader {
public static void main(String[] args) throws IOException {
readProperties();
}
private static void readProperties() throws IOException {
InputStream inputStream = null;
try {
Properties properties = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
inputStream = loader.getResourceAsStream("project.properties");
properties.load(inputStream);
properties.forEach(
(key, value)
->
System.out.println(
key + "=" + value
)
);
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
}
Output – read property file from resource directory in java (example)
user.email=admin@gmail.com
user.country=USA
user.age=25
user.password=password
user.name=admin