如何向 PropertiesConfiguration 文件写入注释?

发布于 2024-12-01 22:32:35 字数 1438 浏览 4 评论 0原文

给定以下实例之一: org.apache.commons.configuration.PropertiesConfiguration 我想写一条评论。如何?

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.");

编辑:我有一个粗略的解决方案。希望它能够得到改进。


这是我能做的最好的事情。它在文件中留下了无关的行。

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 
 }

这种方法的问题之一是 PropertiesConfiguration 文档对布局有点模糊。它没有明确表示注释将出现在虚拟行上方,因此 PropertiesConfiguration 似乎存在在后续调用中可以自由重新排列文件的风险。我什至没有看到保留属性行顺序的保证,因此我无法保证注释(和虚拟行)始终位于注释适用的属性之上:属性label0。当然,我在这里有点偏执。然而,文档确实说布局不能保证保持不变。 希望有人能够想出一些没有虚拟行和 Java 文档或网站保证的东西,以保证评论相对于要评论的属性的位置。编辑:您可能想知道为什么我会这样做创建一个虚拟属性,而不是仅将注释附加到文件中已有的属性之一。原因是因为我想要一个注释来引入属性块,并且可以进行更改(新属性或顺序切换)。我不想造成维护问题。我的评论应该说“这是数据挖掘结果的部分”或“这是时间表的部分”,我永远不必重新访问它。

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 improved 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

混吃等死 2024-12-08 22:32:35

就这样评论?

# This is comment

Comment like this?

# This is comment
倾`听者〃 2024-12-08 22:32:35

PropertiesConfiguration JavaDoc 文档

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

编辑< /strong>:好的,您想从代码中编写注释。也许 - 如果您只需要编写属性文件 - 您可以使用 PropertiesConfiguration.PropertiesWriter 及其 writeComment 像这样的方法:

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();

属性文件将如下所示:

# Example properties
prop1 = foo
prop2 = bar

更新

总结:PropertiesConfiguration 不提供您正在寻找的功能。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文