Friday, May 14, 2010

Property file reader

In my current project we have some property files and we need to read properties from those property files, so we come up with an approach to read property files. The main points that we need to focus was
1.) The properties should be loaded only once, In a way we need singleton behavior
2.) We can have multiple property files
One assumption that we have is property files will always be located in root class path

Now for the solution of this problem we thought of using Enum (effective java ;)) as they internally provide the feature of singleton behavior, also for multiple property file situation we created multiple instances corresponding property file.

So the code that we have written is like this

public enum PropertiesReader {

Properties properties;
private Logger log = LoggerFactory.getLogger(PropertiesReader.class);

private PropertiesReader(String propertyFile) {
properties = new Properties();
try {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(propertyFile);
catch (IOException e) {
log.error(propertyFile + " was not found in classpath");

public String getValue(String key) {
return (String) properties.get(key);

So now
if we want to read a property from file

if we want to read a property from file