使用Java API生成属性文件时,为什么使用反斜杠转义冒号?

fge*_*fge 5 java properties-file

示例代码:

public final class Test
{
    public static void main(final String... args)
        throws IOException
    {
        final Properties properties = new Properties();

        properties.setProperty("foo", "bar:baz");

        // Yeah, this supposes a Unix-like system
        final Path path = Paths.get("/tmp/x.properties");

        try (
            // Requires Java 8!
            final Writer writer = Files.newBufferedWriter(path);
        ) {
            properties.store(writer, null);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,当我:

$ cat /tmp/x.properties 
# The date here
foo=bar\:baz
Run Code Online (Sandbox Code Playgroud)

冒号用反斜杠逃脱.事实上,所有的冒号都是.

奇怪的是,如果我手动生成属性文件并且 "冒走"冒号,那么也会读取属性.

那么,为什么编写过程Properties(无论你使用的方式Writer还是OutputStream顺便使用的方式)以这种方式逃避冒号?

man*_*uti 2

load该类的方法提到Properties以下内容:

该键包含行中从第一个非空白字符开始的所有字符,直到(但不包括)第一个未转义的 '='、 ':'或除行终止符之外的空白字符。所有这些键终止字符都可以通过使用前面的反斜杠字符进行转义来包含在键中

...

作为示例,以下三行中的每一行指定键“Truth”和关联的元素值“Beauty”:

Truth = Beauty

Truth:Beauty

Truth :Beauty

因此,冒号可用于确定属性文件中键的结尾。