【问题标题】:Properties file generation with property comments带有属性注释的属性文件生成
【发布时间】:2016-12-15 16:22:00
【问题描述】:

在一个 Java 程序中,我有一组键值对象,并且想用这些属性编写一个字符串(我将其作为 REST 服务响应返回)。 使用java.util.Properties时一切正常

Properties props = new Properties();
props.put("prop-key", "prop-value");

Writer writer = new StringWriter();
store(writer, "## File comment ##");

这会生成字符串

## File comment ##

prop-key=prop-value

现在我想在每个像这样的属性上添加 cmets

## File comment ##

## Property comment
prop-key=prop-value

但是使用 Java 的属性这是不可能的。除了使用 StringBuilder 或类似的东西之外,我怎样才能完成这项任务?

谢谢

【问题讨论】:

标签: java serialization properties


【解决方案1】:

我以前去过那里...我可以告诉你,来自 Apache 的 Commons Configuration 项目将完成这项工作;它将允许您写入和读取Properties 文件...根据您的要求,请查看PropertiesConfigurationLayout 中的setComment(String key, String comment) 方法。

【讨论】:

    猜你喜欢
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 2015-01-02
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    相关资源
    最近更新 更多