简体   繁体   中英

Read .properties file in java with newline

I know this question is asked many times, but all solutions didn't work for me. The properties files will be read from Ant.

Now I have this value which should displayed as the following:

Germany
Hamburg
Kolonie Str. 14.

So I have the following value in the properties file

myAdress= Germany \n Hamburg \n Kolonie Str. 14.

I also used used all possible brake lines like:

\ or \ \n or \n \ or ${line.separator}

all these possible didn't work instead it display additional empty space instead of new line!! So What I have instead:

Germany  Hamburg   Kolonie Str. 14.

This Properties will be written to xml file and the code will read directly from the xml file, not from properties file.

<property name="confirmText" value="Germany  Hamburg   Kolonie Str. 14." />

Maybe it is XML problem, not from the properties file, but also the XML file dose'nt have any brake line from the properties!! It seems properties file unable to present correct break lines to XML!!

Your problem is, that you cannot put newline characters to XML attributes. Having \\n in your properties file is perfectly right. But you transform this properties file to some proprietary XML format, or at least not to the properties XML format defined by the Properties class, because its format is different and there you could store newlines in values as they are not stored in attributes.

So whatever generates your XML, sets the value from the properties file as attribute value and during this the newlines are replaced by spaces which is expected. You have to escape the newlines, according to the XML format you use. If the software that reads this XML interprets \\n in the attribute as newline character, then either your properts-to-XML converter has to escape the newline characters accordingly, or you have to esacpe the escape character in your properties file, thus having \\\\n which will cause to have \\n as a two-character sequence instead of a newline character and thus it will end as \\n in your XML attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM