且构网

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

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

更新时间:2021-10-29 04:07:36

将属性放入属性对象中,并使用一个 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.