【问题标题】:Apache commons config: Generate configuration file from defaultsApache commons config:从默认值生成配置文件
【发布时间】:2015-05-06 17:09:26
【问题描述】:

我想在一个 jar 中分发我的应用程序。这意味着我不想与它一起发布任何外部文件,这也包括我不想与我的项目一起发布默认配置文件。

我当然可以使用 JDOM2 的实现来构建基本的 XML 结构,或者将其从类路径复制到文件系统中,但是是否有由 Commons Configuration 实现的开箱即用的变体?

我只找到了解释阅读和处理现有配置文件的在线文档。

我可以想象这段代码接受了一些 Key -> Value Pairs 然后生成一个新的配置,这可能吗?

请不要为我提供 XML 文件的任何替代方案,请专门针对我询问的实现回答这个问题。如果您知道任何相关信息,您可以建议替代 Commons Configuration 来完成我需要做的事情。

【问题讨论】:

  • 我的回答有帮助吗?
  • @JonasWolf 哎呀,谢谢,我用过,忘了。谢谢。
  • 谢谢,我只是好奇。我很高兴我的回答很有帮助。

标签: java apache-commons-config


【解决方案1】:

例如呢?

XMLConfiguration config = new XMLConfiguration();
config.addProperty("test.dir[@name]", "C:\\Temp\\");
config.addProperty("test.dir[@name]", "D:\\Data\\");
config.setFilename("yourfilename.xml");
config.save();

查看所有已知的实现类:https://commons.apache.org/proper/commons-configuration/javadocs/v1.10/apidocs/org/apache/commons/configuration/FileConfiguration.html

https://commons.apache.org/proper/commons-configuration/javadocs/v1.10/apidocs/org/apache/commons/configuration/XMLConfiguration.html 为例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 2019-09-20
    • 1970-01-01
    相关资源
    最近更新 更多