7

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

The_Lost_Avatar
  • 992
  • 5
  • 15
  • 35
  • 1
    Is it not working, can you elaborate? – jmiserez Oct 13 '13 at 19:57
  • yes, that's working fine functionality wise, but for the sake of formatting I wanted to keep it that way only. I SO'd and found out that I cannot add multiline comments, But I have no idea what are multiline comments. So I thought that this might be possible. – The_Lost_Avatar Oct 13 '13 at 20:01

3 Answers3

4

You can use the Apache Commons Configuration to write and read Properties files, specifically the setComment() function in a PropertiesConfigurationLayout which allows you to specify a comment for each property.

Note that above links refer to Commons Configuration v1.x, while v2.0 has been released in the meantime, which has different package names.

sryll
  • 114
  • 8
jmiserez
  • 2,991
  • 1
  • 23
  • 34
1

There is no way to do what you want using the standard Properties class.

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.

Paul Wagland
  • 27,756
  • 10
  • 52
  • 74
0

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"); 
user2420898
  • 89
  • 1
  • 2