简体   繁体   English

使用换行符读取Java中的.properties文件

[英]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. 将从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. 此属性将被写入xml文件,并且代码将直接从xml文件而不是从属性文件读取。

<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!! 也许这是XML问题,而不是属性文件中的问题,但XML文件也没有属性问题!! It seems properties file unable to present correct break lines to XML!! 似乎属性文件无法将正确的换行符呈现给XML!

Your problem is, that you cannot put newline characters to XML attributes. 您的问题是,不能将换行符放入XML属性。 Having \\n in your properties file is perfectly right. 在属性文件中使用\\n是完全正确的。 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. 但是,您可以将此属性文件转换为某种专有的XML格式,或者至少不转换为Properties类定义的属性XML格式,因为其格式不同,并且可以在其中存储换行符,因为它们不存储在属性中。

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. 因此,无论生成XML的是什么,都将属性文件中的值设置为属性值,在此期间,换行符将替换为预期的空格。 You have to escape the newlines, according to the XML format you use. 您必须根据所使用的XML格式对换行符进行转义。 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. 如果读取此XML的软件将属性中的\\ n解释为换行符,则您的XML到属性转换器必须相应地对换行符进行转义,或者必须在属性文件中保留转义符,从而使\\\\n将导致\\n是两个字符的序列,而不是换行符,因此在XML属性中将以\\n结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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