逐行在属性文件中添加注释

时间:2013-10-13 19:55:05

标签: java properties

这是我想在属性文件中执行的操作

#Comments about key Value pair 1
Key_1=value_1


#Comments about key Value pair 2
Key_2=value_2

#Comments about key Value pair 3
Key_3=value_3

现在我可以用我的文件做什么

#OMG, It works!
#Mon Oct 14 01:22:10 IST 2013
Key_1=Value_1
Key_2=Value_2

有没有办法做这样的事情

3 个答案:

答案 0 :(得分:4)

您可以使用Apache Commons Configuration来编写和读取属性文件,特别是setComment()中的PropertiesConfigurationLayout函数,它允许您为每个属性指定注释。

请注意,上面的链接指的是Commons Configuration v1.x,而v2.0在此期间已经发布,它具有不同的包名。

答案 1 :(得分:1)

使用标准Properties类无法做任何你想做的事。

当然,您可以通过自己编写文件来完成此操作。您需要注意的主要事项是嵌入式换行符,并在您的键中嵌入=:。但是,如果您真的希望能够存储关于每对的单独注释,那么您可能需要一个从<key>⇒<value,comment>映射的类,然后使用该映射来生成属性文件。

答案 2 :(得分:0)

我认为基本上你想要带有描述评论的财产。如果是这种情况

Properties props=new Properties();
props.add("key","value");
FileOutputStream output=new FileOutputStream("props.dat",true); //so that it won't create a new file since it is 'true')
 props.store(output,"Sample properties");