如何在属性文件中转义冒号(:)? [英] 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.
我知道冒号:
是一个标准分隔符的属性
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?
推荐答案
将属性放入属性
对象中,并使用一个 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:
...对于密钥,所有空格字符都用前面的\\ \\ character。对于元素,前导空格字符,但不是嵌入或尾随空格字符,用前面的\字符写入,键和元素字符#,!,=和;用前面的反斜杠写入,以确保它们正确加载。
只有手动创建/写入文件时,才需要手动转义字符。
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屋!