开发者

How do I write a comment to a PropertiesConfiguration file?

开发者 https://www.devze.com 2023-04-01 04:41 出处:网络
Given one of these instances: org.apache.commons.configuration.PropertiesConfiguration I want to write a comment.How?

Given one of these instances: org.apache.commons.configuration.PropertiesConfiguration I want to write a comment. How?

pc = new PropertiesConfiguration();

writeComment("this is a comment about the stuff below"); // HOW DO I WRITE THIS?
pc.addProperty("label0", myString);
writeComment("end of the stuff that needed a comment.");

Edit: I have a crude solution. Hopefully it can be improv开发者_如何学Ced upon.


Here's the best I could do. It leaves an extraneous line in the file.

pc = new PropertiesConfiguration();
writeComment(pc, "The following needed a comment so this is a comment.");
pc.addProperty(label0, stuff0);
writeComment(pc, "End of the stuff that needed a comment.");

...
 private void writeComment(PropertiesConfiguration pc, String s)
 {
    String propertyName = String.format("%s%d", "comment", this.commentNumber++);

    pc.getLayout().setComment(propertyName, s + " (" + propertyName + ")");

    // make a dummy property 
    pc.addProperty(propertyName, "."); 
         // put in a dummy right-hand-side value so the = sign is not lonely 
 }

One of the problems with this approach is that the PropertiesConfiguration doc is a little vague about the layout. It does not explicitly say that the comment will appear above the dummy line so there seems to be the risk that PropertiesConfiguration is free to re-arrange the file on subsequent invocations. I have not even seen an guarantee that property line order is preserved so I cannot guarantee that the comment (and dummy line) will always be above the property that the comment applies to: property label0. Of course, I'm being a little paranoid here. However, the doc does say that layouts are not guaranteed to remain unmodified. Hopefully somebody can come up with something without the dummy line and a Java doc or website guarantee on the position of the comment relative to the property it is meant to comment on. Edit: You might wonder why I would create a dummy property instead of just attaching the comment to one of the properties that would already be in the file. The reason is because I want a comment to introduce a block of properties and changes (new ones, or a switch in the order) are possible. I don't want to create a maintenance problem. My comment should say "this is the section for data mining results" or "this is the section for the schedule" and I should never have to revisit this.


Comment like this?

# This is comment


The PropertiesConfiguration JavaDoc documents

 Blank lines and lines starting with character '#' or '!' are skipped. 

EDIT: Ok, you want to write the comment from code. Maybe - if you just need to write a property file - you can use the PropertiesConfiguration.PropertiesWriter and its writeComment method like this:

FileWriter writer = new FileWriter("test.properties");
PropertiesWriter propWriter = new PropertiesWriter(writer, ';');

propWriter.writeComment("Example properties");
propWriter.writeProperty("prop1","foo");
propWriter.writeProperty("prop2", "bar");

propWriter.close();

The property file will look like this:

# Example properties
prop1 = foo
prop2 = bar

Update

Summarized: The PropertiesConfiguration does not provide the functionality you are looking for.

0

精彩评论

暂无评论...
验证码 换一张
取 消