Load settings from properties file in Java

Java

This example assume the properties file is located in the same directory where the jar file runs, the properties file is of the form:


key1=value1
key2=value2

First, get the current path, then load the properties


private static void LoadProperties() {
    try {
        String currentPath = new java.io.File(".").getCanonicalPath();
			
        File propsFile = new File(currentPath, "rawsigningserver.properties");
			
        if (!propsFile.exists())
        {
            logger.debug("Properties file not found: " + propsFile.getAbsolutePath());
            return;
        }
			
        InputStream input = new FileInputStream(propsFile.getAbsolutePath());

        properties = new Properties();

        // load a properties file
        properties.load(input);

        // get the property value and print it out
        CACHE_EXPIRE_SEC = Integer.parseInt(properties.getProperty("cache.expire.sec", "" + CACHE_EXPIRE_SEC));
            
        logger.debug("cache expire sec: " + CACHE_EXPIRE_SEC);
	           
    } catch (IOException e) { 
		logger.error("", e);
	}			
}

Post a Comment

Previous Post Next Post