The Apache Commons Configuration has a PropertiesConfiguration property. It supports the function converting delimited string to array/list.
For example, if you have a properties file
With the code below:
PropertiesConfiguration config = new PropertiesConfiguration("Foo.properties"); String[] values = config.getStringArray("foo");
will provide you with a string array ["bar1", "bar2", "bar3"]
getString returns only the first value for a multi-valued key. Try getStringArray to get both values.
Full code:
ApacheCommonPropertyConfig.java
import java.util.List; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; public class ApacheCommonPropertyConfig { public static void main(String[] args) throws ConfigurationException { PropertiesConfiguration config; try { config = new PropertiesConfiguration("F://foo.properties");
foo.properties
foo=bar1, bar2, bar3
Output:
Array Value is: bar1 Array Value is: bar2 Array Value is: bar3 List Value is: bar1 List Value is: bar2 List Value is: bar3 Another List Value is: bar1 Another List Value is: bar2 Another List Value is: bar3
Another example of using AbstractFileConfiguration
ListDelimiterDemo.java
import org.apache.commons.configuration.AbstractFileConfiguration; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; public class ListDelimiterDemo { public static void main(String[] args) throws ConfigurationException { AbstractFileConfiguration config = new PropertiesConfiguration(); config.setListDelimiter(','); config.load("F://foo.properties"); for (Object listItem : config.getList("foo")) { System.out.println(listItem); } } }
Output:
bar1 bar2 bar3
Link to the resource:
source share