我正在使用commons-configuration v1.10,而我正在使用类PropertiesConfiguration
来读取我的应用程序属性。我有一个带有逗号的属性,但是当我读入它时,它会被分隔,我无法弄清楚如何得到它。
它按顺序吐出属性并包含逗号,但它之所以存在问题,是因为我得到'['和']'围绕它。
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 "";
}
}
答案 0 :(得分:0)
发布您的代码会有所帮助。
根据您想要的official docs AbstractConfiguration.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以获取实际属性值的开头始终有效。
答案 1 :(得分:0)
看起来我不能使用Apache Commons或PropertiesConfiguration
来检索属性而不分隔它。但是,java.util.Properties
没有此问题,因此使用该问题代替PropertiesConfiguration
。
MKYong有一个很好的例子,如何设置它,http://www.mkyong.com/java/java-properties-file-examples/
答案 2 :(得分:0)
PropertiesConfiguration properties = new PropertiesConfiguration();
properties.setDelimiterParsingDisabled(true);
上面的代码解决了这个问题,没有必要切换到java.util.Properties。层次结构如下:
PropertiesConfiguration
|
|(extends)
|
AbstractFileConfiguration
|
|(extends)
|
BaseConfiguration
|
|(extends)
|
AbstractConfiguration
就我而言,我特别使用Apache Properties Configuration,因为它支持变量替换,java.util.Properties不支持
答案 3 :(得分:0)
它可以工作,但是在加载配置之前,您应该禁用它或对其进行设置。
PropertiesConfiguration config = new PropertiesConfiguration();
config.setDelimiterParsingDisabled(true)
config.setListDelimiter(';');
config.setFile(new File("application.properties"));
config.load();