Property Manger java code provides sample java method to load the properties from a propertyfile and cache it in a singletone object. Code shown here can be copied from here and used in any java project. It uses java.util.Properties class e.
Java Property Manager File Content:
package test.easycodeforall.singletone;
import java.io.InputStream;
import java.util.Properties;
public final class PropManager {
private static PropManager obj = new PropManager();
private Properties prop = null;
private String propFileName = "easycodeforall.properties";
private PropManager() {
loadProperties();
}
public void refreshCache() {
loadProperties();
}
private void loadProperties() {
try {
prop = new Properties();
InputStream propAsStream = this.getClass().getResourceAsStream(propFileName);
prop.load(propAsStream);
} catch (Exception ex) {
System.out.println("Unable to load properties from " + propFileName + ". Exception=" + ex);
}
}
public static PropManager getInstance() {
return obj;
}
public String getProperty(String propertyName) {
return prop.getProperty(propertyName);
}
}
Please provide your feedback here