2016-08-17 41 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