2016-08-17 63 views
0

我正在使用Apache公共配置库来读取属性文件出于各种原因。我的代码如下。默认情况下,它将删除值之前和之后的空格,这是我想要避免的。有没有办法做到这一点? samplePropertiesFile.properties的修改Apache CommonConfiguration以不修剪值

CompositeConfiguration config = new CompositeConfiguration(); 
PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration(); 
URL url = CommonConfig.class.getResource("samplePropertiesFile.properties"); 
Reader reader = new FileReader(new File(url.getFile())); 
propertiesConfiguration.read(reader); 
config.addConfiguration(propertiesConfiguration); 

内容:

key=<space>value<space> 

//两个空间之前,必须保存好。

回答

0

值为Java转义,所以使用\u0020作为不应删除的前导和尾随空格。

只有第一个最后一个尾部空间需要这个,例如,

key = \u0020 value \u0020 

将字符串<sp><sp><sp>value<sp><sp><sp>

(OR)

写CustomPropertiesConfiguration像在 “自定义属性读取器和写入” 部分的例子在 https://commons.apache.org/proper/commons-configuration/userguide/howto_properties.html

1)根据source code

+0

这是一个可能的解决办法。谢谢你。有没有办法保持属性文件完好,并通过代码更改实现解决方案? – user3366706

+0

是的,当然有。你...... eehhhhh .... *更改代码!!?!?!!?*第1步:获取源代码。第2步:进行更改。第3步:编译并构建新的jar文件。第4步:在你的类路径中替换jar文件。 – Andreas

+0

hmmmmm ...我期待着这样的事情 - https://commons.apache.org/proper/commons-configuration/userguide/howto_properties.html上的自定义属性阅读器和编写器部分,它是用于PropertiesConfiguration的,而不是CompositeConfiguration – user3366706