在Java中读取.properties文件,并在值中添加新的行字符 [英] Read .properties file in java with new line characters in values

查看:231
本文介绍了在Java中读取.properties文件,并在值中添加新的行字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一个.properties文件,其属性如下

  A = Hello World这是一个虚拟文本

B =再见世界我要离开火星

我能够正确阅读在输入流中加载资源并显示好之后。



现在我想定义这样的属性这样的

  A = Hello World 
这是一个
虚拟文本

B = Bye Bye World
我要离开
mars

但是它不按预期加载它

  A作为Hello World 

和<作为再见世界,p>

  B。 

我必须自己定义正则表达式并阅读并填写地图。 / p>

或者在 Properties.java 中有什么默认可用?

解决方案

您需要添加一个 \\\
,然后用继续字符结束行: \

 
A = Hello World\\\
\
这是一个\\\
\
虚拟文本

请注意,加载属性文件时会导致空白空格。因此,如果需要缩进行,则需要将其添加到上一行的末尾:

 
A = Hello World\\\
\
这是一个\\\
\
虚拟文本


I defined a .properties file with properties like this

A=Hello World this is a dummy text

B=Bye Bye World I am leaving to mars

I am able to read it properly after loading resource from input stream and display them good.

Now i want to define these properties like this

A= Hello World 
   this is a 
   dummy text

B= Bye Bye World 
   I am leaving to 
   mars

However it don't load it as expected

A as Hello World

and

B as Bye Bye World.

Do i have to go by myself defining a regex and read this and fill it in a map.

Or is there any way default available in Properties.java?

解决方案

You need to add a \n and then end the line with the "continuation" character: \

A=Hello World\n \
this is a\n \
dummy text

Note that leading whitespace is trimmed when loading the Properties file. So you need to add that to the end of the previous line if you need indented lines:

A=Hello World\n   \
this is a\n    \
dummy text

这篇关于在Java中读取.properties文件,并在值中添加新的行字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆