Sample Property Reader in Java
public class PropertyReader {
private static Properties properties = new Properties();
static{
try{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("filename.properties");
properties = new Properties();
properties.load(inputStream);
inputStream.close();
}catch(Exception ex)
{
ex.printStackTrace();
}
}
public static boolean containsKey(String key)
{
if(properties.containsKey(key)) {
return true;
}
return false;
}
public static String getPropertyValue(String key)
{
if(properties.containsKey(key)) {
return (String) properties.get(key);
}
return null;
}
public static void setPropertyValue(String key, String value)
{
properties.setProperty(key, value);
}
public static String getPropertyValue(String key, String defaultValue)
{
return properties.get(key) == null ? defaultValue:(String) properties.get(key) ;
}
}
Comments
Post a Comment