将注释属性加载到 java 中的 Properties 对象
Load commented properties to Properties object in java
我正在尝试使用 load(new FileReader())
方法将属性加载到 java 中的 Properties 对象。除以 (#) 注释开头的属性外,所有属性均已加载。如何使用 java API 将这些注释属性加载到 Properties 对象。只能手动方式吗?
提前致谢。
我可以建议你扩展 java.util.Properties
class 来覆盖这个特性,但它不是为它设计的:很多东西都是硬编码的,不能覆盖。因此,您应该对方法进行完整的复制粘贴,只需稍作修改。
例如,有时,内部使用的 LineReader 在您加载属性文件时执行此操作:
if (isNewLine) {
isNewLine = false;
if (c == '#' || c == '!') {
isCommentLine = true;
continue;
}
}
#
是硬编码的。
编辑
另一种方法可以逐行读取属性文件,如果第一个字符是#
,则删除第一个字符,然后将读取的行写入ByteArrayOutputStream
,如果需要可以修改。然后你可以用 ByteArrayInputStream
从 ByteArrayOutputStream.toByteArray()
.
加载属性
这里有一个可能的单元测试实现:
作为输入 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"));
}
我正在尝试使用 load(new FileReader())
方法将属性加载到 java 中的 Properties 对象。除以 (#) 注释开头的属性外,所有属性均已加载。如何使用 java API 将这些注释属性加载到 Properties 对象。只能手动方式吗?
提前致谢。
我可以建议你扩展 java.util.Properties
class 来覆盖这个特性,但它不是为它设计的:很多东西都是硬编码的,不能覆盖。因此,您应该对方法进行完整的复制粘贴,只需稍作修改。
例如,有时,内部使用的 LineReader 在您加载属性文件时执行此操作:
if (isNewLine) {
isNewLine = false;
if (c == '#' || c == '!') {
isCommentLine = true;
continue;
}
}
#
是硬编码的。
编辑
另一种方法可以逐行读取属性文件,如果第一个字符是#
,则删除第一个字符,然后将读取的行写入ByteArrayOutputStream
,如果需要可以修改。然后你可以用 ByteArrayInputStream
从 ByteArrayOutputStream.toByteArray()
.
这里有一个可能的单元测试实现:
作为输入 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"));
}