Property Reader In Java

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

Popular posts from this blog

Get rid of "Timed out receiving message from renderer"

Setup Selenium Grid using Kubernetes

Launching Selenium Grid and Registering a Web Node