Hi,
Here, a simple java example using the Apache Commons Configuration library. The Apache Commons Configuration software library provides a generic configuration interface which enables a Java application to read configuration data from a variety of sources.
Some resources:
https://commons.apache.org/proper/commons-configuration/
https://commons.apache.org/proper/commons-configuration/userguide/quick_start.html
A simple file properties myfile.properties:
MYPARAMETER1=VALUE1 MYPARAMETER2=VALUE2 MYPARAMETER3=VALUE3 MYPARAMETER4_INT=123 MYPARAMETER5_DOUBLE=123.45 MYPARAMETER6_BOOL=true
We need the librairies commons-configuration-1.5.jar and commons-collections-3.2.jar:
package com.huo.test.javaconfig; import java.text.MessageFormat; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy; /** * Class Test for test loading configuration */ public class TestConfig { public static void main(String[] args) { try { System.out.println(" ############################## EXECUTION "+TestConfig.class+" - START ############################## "); PropertiesConfiguration docbaseConfig = new PropertiesConfiguration(MessageFormat.format("{0}/src/com/huo/test/javaconfig/myfile.properties", System.getProperty("user.dir"))); docbaseConfig.setListDelimiter(','); docbaseConfig.setReloadingStrategy(new FileChangedReloadingStrategy()); { final String parameter1 = docbaseConfig.getString("MYPARAMETER1"); System.out.println("The value of MYPARAMETER1 is :"+parameter1); final String parameter2 = docbaseConfig.getString("MYPARAMETER2", ""); System.out.println("The value of MYPARAMETER2 is :"+parameter2); final String parameter3 = docbaseConfig.getString("MYPARAMETER3", ""); System.out.println("The value of MYPARAMETER3 is :"+parameter3); // final int parameter4 = docbaseConfig.getInt("MYPARAMETER4_INT"); System.out.println("The value of MYPARAMETER4_INT is :"+parameter4); final double parameter5 = docbaseConfig.getDouble("MYPARAMETER5_DOUBLE"); System.out.println("The value of MYPARAMETER5_DOUBLE is :"+parameter5); final boolean parameter6 = docbaseConfig.getBoolean("MYPARAMETER6_BOOL"); System.out.println("The value of MYPARAMETER6_BOOL is :"+parameter6); } } catch (Throwable e) { e.printStackTrace(); }finally{ System.out.println(" ############################## EXECUTION "+TestConfig.class+" - END ############################## "); } } }
The outputs are:
############################## EXECUTION class com.huo.test.javaconfig.TestConfig - START ############################## The value of MYPARAMETER1 is :VALUE1 The value of MYPARAMETER2 is :VALUE2 The value of MYPARAMETER3 is :VALUE3 The value of MYPARAMETER4_INT is :123 The value of MYPARAMETER5_DOUBLE is :123.45 The value of MYPARAMETER6_BOOL is :true ############################## EXECUTION class com.huo.test.javaconfig.TestConfig - END ##############################
That’s all!!
Huseyin OZVEREN