Java 在属性文件中逐行添加注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19349585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding comments in a properties file line by line
提问by The_Lost_Avatar
This is something which I want to do in properties file
这是我想在属性文件中做的事情
#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
Right now what I am able to do with my file is
现在我可以用我的文件做的是
#OMG, It works!
#Mon Oct 14 01:22:10 IST 2013
Key_1=Value_1
Key_2=Value_2
Is there any way of doing such a thing
有没有办法做这样的事情
采纳答案by jmiserez
You can use the Apache Commons Configurationto write and read Properties files, specifically the setComment()function in a PropertiesConfigurationLayoutwhich allows you to specify a comment for each property.
您可以使用Apache的共享配置写入和读取属性的文件,特别是setComment()函数中的PropertiesConfigurationLayout它允许您指定每个属性的注释。
Note that above links refer to Commons Configuration v1.x, while v2.0 has been released in the meantime, which has different package names.
注意上面的链接是指Commons Configuration v1.x,同时发布了v2.0,包名不同。
回答by Paul Wagland
There is no way to do what you want using the standard Propertiesclass.
使用标准Properties类无法做您想做的事情。
You can, of course, do this by writing the file out yourself. The main thing that you need to be careful of is embedded newlines, and embedded =
and :
in your keys. However, if you really want to be able to store individual comments about each pair, then you probably want a class that maps from <key>?<value,comment>
, and then use that map to generate your properties file.
当然,您可以通过自己写出文件来做到这一点。你需要小心的是嵌入换行符和嵌入的主要事情=
,并:
在你的钥匙。但是,如果您真的希望能够存储关于每一对的单独注释,那么您可能需要一个从 映射的类,<key>?<value,comment>
然后使用该映射来生成您的属性文件。
回答by user2420898
I think basically you want the property with a comment with description. If that is the case
我认为基本上你想要带有描述的评论的属性。如果是这样的话
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");