Java 如何在不分隔属性的情况下获取属性?

Java 如何在不分隔属性的情况下获取属性?,java,apache-commons,apache-commons-config,Java,Apache Commons,Apache Commons Config,我正在使用commons configuration v1.10,我正在使用类属性配置来读取我的应用程序属性。我有一个属性,其中有逗号,但当我读入它时,它被分隔,我不知道如何使它不被分隔 它按顺序和逗号返回属性,但问题的原因是它周围有“[”和“]” AbstractConfiguration有一个函数,setDelimiterParsingDisabled(),该函数禁用定界,但我找不到一个实现它来读取属性文件的类 private static String readProperty(Strin

我正在使用commons configuration v1.10,我正在使用类
属性配置
来读取我的应用程序属性。我有一个属性,其中有逗号,但当我读入它时,它被分隔,我不知道如何使它不被分隔

它按顺序和逗号返回属性,但问题的原因是它周围有“[”和“]”

AbstractConfiguration
有一个函数,
setDelimiterParsingDisabled()
,该函数禁用定界,但我找不到一个实现它来读取属性文件的类

private static String readProperty(String property) {
    try {
        Configuration configuration = new PropertiesConfiguration(propertiesFile);
        return configuration.getProperty(property).toString();
    }
    catch(ConfigurationException e) {
        System.out.println("Issue reading " + property + " property");
        e.printStackTrace();
        System.exit(1);
        return "";
    }
}

发布你的代码会有帮助

根据您想要的
抽象配置.setListDelimiter(null)

您还可以使用
String
方法查找并删除周围的[]。假设属性位于名为
prop
的字符串中:

int start = prop.indexOf('[') + 1;
int end = prop.lastIndexOf(']');
String val = prop.substring(start,
    end > 0 ? end : prop.length());

indexOf
如果找不到字符,则返回-1,因此添加1以获取实际属性值的开头总是有效的,即使分隔符不存在。

看起来我无法使用Apache Commons或
PropertiesConfiguration
在不进行分隔的情况下检索属性。但是,
java.util.Properties
没有这个问题,所以使用它来代替
PropertiesConfiguration

MKYong有一个很好的例子说明了如何设置它

上面的代码解决了这个问题,不需要切换到java.util.Properties。层次结构如下:

    PropertiesConfiguration
              |
              |(extends)
              |
    AbstractFileConfiguration
              |
              |(extends)
              |
    BaseConfiguration
              |
              |(extends)
              |
    AbstractConfiguration

在我的例子中,我特别使用了Apache属性配置,因为它支持java.util.Properties中不支持的变量替换,它可以工作,但在加载配置之前应该禁用或设置它

PropertiesConfiguration config = new PropertiesConfiguration();
config.setDelimiterParsingDisabled(true)
config.setListDelimiter(';');
config.setFile(new File("application.properties"));
config.load();

我已经在原来的帖子中发布了我的代码。如果你需要我发布更多的代码,请告诉我。手动移除支架是我最后的选择。
PropertiesConfiguration config = new PropertiesConfiguration();
config.setDelimiterParsingDisabled(true)
config.setListDelimiter(';');
config.setFile(new File("application.properties"));
config.load();