且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在属性文件中转义冒号 (:)?

更新时间:2022-06-20 04:08:07

将属性放入 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.