有时 Properties.load() 跳过行

Sometimes Properties.load() skip lines

Properties.load() 在以下情况下跳过 InputStream 的第二行。这是 Java 的错误还是正常行为?

public class PropTest {
    public static void main(String[] args) throws Exception {
        propTest("Test1", "prop=cat,\" + System.lineSeparator() + "dog");
        propTest("Test2", "prop=cat,\  " + System.lineSeparator() + "dog");
    }
    public static void propTest(String name, String test) throws Exception {
        ByteArrayInputStream stream = new ByteArrayInputStream(test.getBytes());
        Properties properties = new Properties();
        properties.load(stream);
        System.out.println(properties.get("prop"));
    }
}

运行 此代码产生以下结果:

猫狗

猫,

根据 Properties file format

A property value can span several lines if each line is terminated by a backslash (‘\’) character.

您在第二个版本中有尾随 space。所以这条线没有继续。注意whitespace规则,有点乱。

White space that appears between the property name and property value is ignored,

White space at the beginning of the line is also ignored.

所以这意味着行尾的白色 space 没有被明确忽略。