Properties file - The reader class
Now we we need a PropertyReader class that would help us read the property values.
Consider the following code for reading the properties. Add this class under the util package.
public class PropertyReader {
Properties properties = new Properties();
InputStream inputStream = null;
public PropertyReader() {
loadProperties();
}
private void loadProperties() {
try {
inputStream = new FileInputStream("src/config.properties");
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public String readProperty(String key) {
return properties.getProperty(key);
}
}
Important points to note in the property reader class.
- Whenever a new instance of this class is created, all the properties would be loaded.
- readProperty() method would be used by implementers to read a property value.