Adding comments in a properties file line by line

后端 未结 3 654
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-18 03:02

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

#Com         


        
相关标签:
3条回答
  • 2021-01-18 03:40

    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.

    0 讨论(0)
  • 2021-01-18 03:43

    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"); 
    
    0 讨论(0)
  • 2021-01-18 03:51

    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.

    0 讨论(0)
提交回复
热议问题