How to write multiple line property value using PropertiesConfiguration?

心已入冬 提交于 2019-12-17 17:54:11

问题


I have a properties file with a property with a List value (comma separated), how to write this property in a multi-line ? (backslash after the comma)?

I can't find anything about this or at least about escaping comma to comma and backslash.


回答1:


If you mean the following; that just relies on backslash + end-of-line. I just found it documented in: http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html

primes = 2,\
    3,\
    5,\
    7,\
    11



回答2:


Check the User Guide for Properties files:

Special Characters and Escaping:

If you need a special character in a property like a line feed, a tabulation or an unicode character, you can specify it with the same escaped notation used for Java Strings. The list separator ("," by default), can also be escaped:

key = This \n string \t contains \, escaped \\ characters \u0020

Backslashes are more difficult.

Lists and arrays:

You can specify a list of values in your properties file by using the same key on several lines:

# chart colors
colors.pie = #FF0000;
colors.pie = #00FF00;
colors.pie = #0000FF;



回答3:


You need to combine the \n character inside the content and the line continuation escape (\<eol> at end of line) to get a multi line property actually be represented in the properties file and in the returned value:

KEY1=first line\n\
second line\n\
last line
KEY2=another key

Not sure if commons-configuration can be configured to actually use this syntax for writing.




回答4:


Another option could be is to use one of properties formats that is designed to support multi-line values.

XML can handle multi-line properties well, but it has a lot of noise.

MProps: is an example of the format with almost no special formatting required: https://github.com/mprops/mprops-java



来源:https://stackoverflow.com/questions/8975908/how-to-write-multiple-line-property-value-using-propertiesconfiguration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!