【问题标题】:Given a Configuration object, how can I create a PropertiesConfiguration object?给定一个 Configuration 对象,我如何创建一个 PropertiesConfiguration 对象?
【发布时间】:2013-08-06 20:48:01
【问题描述】:

我有一个返回配置对象的方法。我需要实例化一个 PropertiesConfig 对象以将其保存到磁盘。我该怎么做?

【问题讨论】:

  • 在此处阅读“保存”部分:commons.apache.org/proper/commons-configuration/…
  • 谢谢 - 我已经读过了,但我拥有的 Config 对象实际上是一个 CombinedConfiguration,并且没有保存方法。我需要将其“转换”为属性配置并将其保存为属性文件。

标签: java configuration properties-file apache-commons-config


【解决方案1】:

您是否尝试过使用复制方法?这个例子对我有用:

    XMLConfiguration conf1 = new XMLConfiguration("table1.xml");
    XMLConfiguration conf2 = new XMLConfiguration("table2.xml");

    // Create and initialize the node combiner
    NodeCombiner combiner = new UnionCombiner();
    combiner.addListNode("table");  // mark table as list node
                // this is needed only if there are ambiguities

    // Construct the combined configuration
    CombinedConfiguration cc = new CombinedConfiguration(combiner);
    cc.addConfiguration(conf1, "tab1");
    cc.addConfiguration(conf2);

    PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties");

    config.copy(cc);
    config.save();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    相关资源
    最近更新 更多