微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在java中使用configuration2读取逗号分隔的属性

我有这个属性
move.patternfile.include = *1a.txt,*2a.txt

我正在尝试使用Apache commons configuration2将其放入列表中.

我的代码是:

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

我可以阅读所有其他属性,但我想要的仍然是1个大小的列表.

这是检索值的命令:

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

你能帮帮忙吗?

解决方法

基于 this,似乎必须在从文件读取属性之前设置分隔符.下面的代码在我运行它时起作用,但会生成警告.
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

产量

2

* 1a.txt

* 2a.txt

这是我运行时看到的警告:

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)

我发现有关此警告消息的this link.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐