Wednesday, 1 February 2017

Do you want to read property file ?

Many of us wanted to read property file as part of framework development and here we go.

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Class to read said property file and load in Properties object
 */
public class PropertiesReaderUtility {

private Properties properties = new Properties();

/**
* constructor which takes file path as input
*
* @param fileName
*/
public PropertiesReaderUtility(String filePath) {
try {
properties.load(new FileInputStream(filePath));
} catch (IOException e) {
throw new RuntimeException(e);
}
assert !properties.isEmpty();
}

/**
* gets the value for the said key
*
* @param key
* @return
*/
public String getProperty(final String key) {
String property = properties.getProperty(key);
return property != null ? property.trim() : property;
}
}

No comments:

Post a Comment