如何在属性文件中转义冒号 (:)? [英] How do you escape colon (:) in Properties file?
问题描述
我正在使用一个属性文件来存储我的应用程序的配置值.在其中一种情况下,我必须将值存储为xxx:yyy:zzz
.当我这样做时,冒号用反斜杠转义 导致值在属性文件中显示为
xxx:yyy:zzz
.
I am using a properties file to store my application's configuration values.
In one of the instances, I have to store a value as
xxx:yyy:zzz
. When I do that, the colon is escaped with a back slash resulting in the value showing as
xxx:yyy:zzz
in the properties file.
我知道冒号 :
是 Properties
Java 类的标准分隔符.但是我仍然需要保存没有反斜杠 的值.
I am aware that the colon :
is a standard delimiter of the Properties
Java class. However I still need to save the value without the back slash .
关于如何处理这个问题有什么建议吗?
Any suggestions on how to handle this?
推荐答案
将属性放入 Properties
对象并使用 store(...)
方法保存.该方法将执行任何所需的转义.Java 文档说:
Put the properties into the Properties
object and save it using a store(...)
method. The method will perform any escaping required. The Java documentation says:
"... 对于键,所有空格字符都以字符开头.对于元素,前导空格字符,但不包括嵌入或尾随空格字符,以字符开头.键和元素字符 #、!、= 和 : 用前面的反斜杠写入,以确保它们被正确加载."
如果您手动创建/编写文件,则只需手动转义字符.
You only need to manually escape characters if you are creating / writing the file by hand.
相反,如果您希望文件包含未转义的冒号字符,那您就不走运了.此类文件格式错误,可能无法使用 Properties.load(...)
方法正确加载.如果您沿着这条路线走下去,您将需要实现自己的自定义加载和/或存储方法.
Conversely, if you want the file to contain unescaped colon characters, you are out of luck. Such a file is malformed and probably won't load properly using the Properties.load(...)
methods. If you go down this route, you'll need to implement your own custom load and/or store methods.
这篇关于如何在属性文件中转义冒号 (:)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!