在java中将注释属性加载到properties对象

在java中将注释属性加载到properties对象,java,properties,load,comments,Java,Properties,Load,Comments,我正在尝试使用load(newfilereader())方法将属性加载到java中的properties对象。除以(#)注释的属性开头的属性外,所有属性均已加载。如何使用JavaAPI将这些注释属性加载到properties对象。只有手动方式吗 提前感谢。我可以建议您扩展java.util.Properties类来覆盖这些特性,但它不是为它而设计的:很多东西都是硬编码的,不可覆盖。因此,您应该只需稍加修改即可完成方法的整个复制粘贴。 例如,在加载特性文件时,内部中使用的LineReader有时会

我正在尝试使用
load(newfilereader())
方法将属性加载到java中的properties对象。除以(#)注释的属性开头的属性外,所有属性均已加载。如何使用JavaAPI将这些注释属性加载到properties对象。只有手动方式吗


提前感谢。

我可以建议您扩展
java.util.Properties
类来覆盖这些特性,但它不是为它而设计的:很多东西都是硬编码的,不可覆盖。因此,您应该只需稍加修改即可完成方法的整个复制粘贴。 例如,在加载特性文件时,内部中使用的LineReader有时会执行以下操作:

 if (isNewLine) {
                isNewLine = false;
                if (c == '#' || c == '!') {
                    isCommentLine = true;
                    continue;
                }
 }
#
是硬编码的

编辑

另一种方法是逐行读取属性文件,删除第一个字符(如果它是
#
),然后将读取的行写入
ByteArrayOutputStream
,如果需要可以修改。然后,您可以从
ByteArrayOutputStream.toByteArray()
加载带有
ByteArrayInputStream
的属性

下面是一个可能的单元测试实现:

@Test
public void loadAllPropsIncludingCommented() throws Exception {

    // check properties commented not retrieved
    Properties properties = new Properties();
    properties.load(LoadCommentedProp.class.getResourceAsStream("/myProp.properties"));
    Assert.assertEquals("woof", properties.get("dog"));
    Assert.assertNull(properties.get("cat"));

    // action
    BufferedReader bufferedIs = new BufferedReader(new FileReader(LoadCommentedProp.class.getResource("/myProp.properties").getFile()));
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    String currentLine = null;
    while ((currentLine = bufferedIs.readLine()) != null) {
        currentLine = currentLine.replaceFirst("^(#)+", "");
        out.write((currentLine + "\n").getBytes());
    }
    bufferedIs.close();
    out.close();

    // assertion
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    properties = new Properties();
    properties.load(in);
    Assert.assertEquals("woof", properties.get("dog"));
    Assert.assertEquals("meow", properties.get("cat"));
}
作为输入
myProp.properties

dog=woof
#cat=meow
单元测试:

@Test
public void loadAllPropsIncludingCommented() throws Exception {

    // check properties commented not retrieved
    Properties properties = new Properties();
    properties.load(LoadCommentedProp.class.getResourceAsStream("/myProp.properties"));
    Assert.assertEquals("woof", properties.get("dog"));
    Assert.assertNull(properties.get("cat"));

    // action
    BufferedReader bufferedIs = new BufferedReader(new FileReader(LoadCommentedProp.class.getResource("/myProp.properties").getFile()));
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    String currentLine = null;
    while ((currentLine = bufferedIs.readLine()) != null) {
        currentLine = currentLine.replaceFirst("^(#)+", "");
        out.write((currentLine + "\n").getBytes());
    }
    bufferedIs.close();
    out.close();

    // assertion
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    properties = new Properties();
    properties.load(in);
    Assert.assertEquals("woof", properties.get("dog"));
    Assert.assertEquals("meow", properties.get("cat"));
}