简体   繁体   English

有时 Properties.load() 跳过行

[英]Sometimes Properties.load() skip lines

Properties.load() skip the second line of InputStream in the following case. Properties.load() 在以下情况下跳过 InputStream 的第二行。 Is it a bug or normal behaviour of Java?这是 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"));
    }
}

Running this code results the following:运行此代码会产生以下结果:

cat,dog猫狗

cat,猫,

According to the Properties file format根据属性文件格式

A property value can span several lines if each line is terminated by a backslash ('\') character.如果每行以反斜杠 ('\') 字符结尾,则属性值可以跨越多行。

You have a trailing space in the second version.您在第二个版本中有一个尾随空格。 So the line isn't continued.所以这条线没有继续。 Note the whitespace rules, which are a bit confusing.请注意空格规则,这有点令人困惑。

White space that appears between the property name and property value is ignored,属性名称和属性值之间出现的空白将被忽略,

and

White space at the beginning of the line is also ignored.行首的空白也被忽略。

So that means white space at the end of the line isn't explicitly ignored.所以这意味着行尾的空白不会被明确忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM