Reading comma separated properties with configuration2 in java

I have this property:

move.patternfile.include = *1a.txt,*2a.txt 

and I'm trying to put it on a list using the Apache2 community configuration.

The code I have is:

 Configurations configs = new Configurations(); AbstractConfiguration config = configs.properties(new File(fileName)); config.setListDelimiterHandler(new DefaultListDelimiterHandler(',')); 

I can read all the other properties, but the one I want is still a list of 1 size.

This is the command to get the values:

 List<String> linclude = configuration.getList(String.class, "patternfile.include"); 

You can help?

+6
source share
2 answers

Based on this , it seems that the delimiter should be set before the properties are read from the file. The code below works when I run it, but generates a warning.

 Parameters params = new Parameters(); FileBasedConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<PropertiesConfiguration>( PropertiesConfiguration.class).configure(params.fileBased() .setListDelimiterHandler(new DefaultListDelimiterHandler(',')) .setFile(new File("test.properties"))); PropertiesConfiguration config = builder.getConfiguration(); List<String> linclude = config.getList(String.class, "patternfile.include"); System.out.println(linclude.size()); for(String item: linclude){ System.out.println(item); } 

test.properties

patternfile.include = * 1a.txt, * 2a.txt

Exit

2

* 1a.txt

* 2a.txt

Here is the warning I see at startup:

 Jun 26, 2016 2:12:17 AM org.apache.commons.beanutils.FluentPropertyBeanIntrospector introspect WARNING: Error when creating PropertyDescriptor for public final void org.apache.commons.configuration2.AbstractConfiguration.setProperty(java.lang.String,java.lang.Object)! Ignoring this property. java.beans.IntrospectionException: bad write method arg count: public final void org.apache.commons.configuration2.AbstractConfiguration.setProperty(java.lang.String,java.lang.Object) at java.beans.PropertyDescriptor.findPropertyType(Unknown Source) at java.beans.PropertyDescriptor.setWriteMethod(Unknown Source) at java.beans.PropertyDescriptor.<init>(Unknown Source) at org.apache.commons.beanutils.FluentPropertyBeanIntrospector.createFluentPropertyDescritor(FluentPropertyBeanIntrospector.java:177) at org.apache.commons.beanutils.FluentPropertyBeanIntrospector.introspect(FluentPropertyBeanIntrospector.java:140) at org.apache.commons.beanutils.PropertyUtilsBean.fetchIntrospectionData(PropertyUtilsBean.java:2234) at org.apache.commons.beanutils.PropertyUtilsBean.getIntrospectionData(PropertyUtilsBean.java:2215) at org.apache.commons.beanutils.PropertyUtilsBean.getPropertyDescriptor(PropertyUtilsBean.java:950) at org.apache.commons.beanutils.PropertyUtilsBean.isWriteable(PropertyUtilsBean.java:1466) at org.apache.commons.configuration2.beanutils.BeanHelper.isPropertyWriteable(BeanHelper.java:521) at org.apache.commons.configuration2.beanutils.BeanHelper.initProperty(BeanHelper.java:357) at org.apache.commons.configuration2.beanutils.BeanHelper.initBeanProperties(BeanHelper.java:273) at org.apache.commons.configuration2.beanutils.BeanHelper.initBean(BeanHelper.java:192) at org.apache.commons.configuration2.beanutils.BeanHelper$BeanCreationContextImpl.initBean(BeanHelper.java:669) at org.apache.commons.configuration2.beanutils.DefaultBeanFactory.initBeanInstance(DefaultBeanFactory.java:162) at org.apache.commons.configuration2.beanutils.DefaultBeanFactory.createBean(DefaultBeanFactory.java:116) at org.apache.commons.configuration2.beanutils.BeanHelper.createBean(BeanHelper.java:459) at org.apache.commons.configuration2.beanutils.BeanHelper.createBean(BeanHelper.java:479) at org.apache.commons.configuration2.beanutils.BeanHelper.createBean(BeanHelper.java:492) at org.apache.commons.configuration2.builder.BasicConfigurationBuilder.createResultInstance(BasicConfigurationBuilder.java:447) at org.apache.commons.configuration2.builder.BasicConfigurationBuilder.createResult(BasicConfigurationBuilder.java:417) at org.apache.commons.configuration2.builder.BasicConfigurationBuilder.getConfiguration(BasicConfigurationBuilder.java:285) at main.Main.main(Main.java:25) 

I found this link regarding this warning message.

+4
source

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

 #Foo.properties foo=bar1, bar2, bar3 

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"); // For Array String[] values = config.getStringArray("foo"); for(String strVal : values) { System.out.println("Array Value is: "+strVal); } // For List List<Object> linclude = config.getList("foo"); for(Object str : linclude){ System.out.println("List Value is: "+str.toString()); } // For List Another List<Object> list = config.getList("listOfValue", config.getList("foo")); for(Object str : list){ System.out.println("Another List Value is: "+str.toString()); } } catch (Exception e) { e.printStackTrace(); } } } 

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:

+2
source

All Articles