Read comma separated attributes in Java using configuration2
•
Java
I have this attribute:
move.patternfile.include = *1a.txt,*2a.txt
I'm trying to put it in the list using Apache Commons configuration2
My code is:
Configurations configs = new Configurations();
AbstractConfiguration config = configs.properties(new File(fileName));
config.setListDelimiterHandler(new DefaultListDelimiterHandler(','));
I can read all the other attributes, but what I want is still a 1 - size list
This is the command to retrieve values:
List<String> linclude = configuration.getList(String.class,"patternfile.include");
Can you help me?
Solution
Based on this, it seems that the delimiter must be set before reading properties from the file The following code 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
yield
two
* 1a. txt
* 2a. txt
This is the warning I see when I run:
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 about this warning message
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
