java 一个更好的类来更新属性文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/565932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:53:21  来源:igfitidea点击:

A better class to update property files?

javaproperties

提问by Miserable Variable

Though java.util.propertiesallows reading and writing properties file, the writing does not preserve the formatting. Not surprising, because it is not tied to the property file.

虽然java.util.properties允许读写属性文件,但写入不保留格式。这并不奇怪,因为它与属性文件无关。

Is there a PropertyFileclass out there -- or some such -- that preserves comments and blank lines and updates property values in place?

是否有一个PropertyFile类 - 或类似的 - 保留注释和空行并更新适当的属性值?

回答by Il-Bhima

It doesn't get much better than Apache's Commons ConfigurationAPI. This provides a unified approach to configuration from Property files, XML, JNDI, JDBC datasources, etc.

它并没有比 Apache 的 Commons ConfigurationAPI好多少。这提供了从属性文件、XML、JNDI、JDBC 数据源等进行配置的统一方法。

It's handling of property files is very good. It allows you to generate a PropertiesConfigurationLayoutobject from your property which preserves as much information about your property file as possible (whitespaces, comments etc). When you save changes to the property file, these will be preserved as best as possible.

它对属性文件的处理非常好。它允许您从您的属性生成一个PropertiesConfigurationLayout对象,该对象尽可能多地保留有关您的属性文件的信息(空格、注释等)。当您保存对属性文件的更改时,这些更改将尽可能保留。



Sample code:

示例代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.PropertiesConfigurationLayout;

public class PropertiesReader {
    public static void main(String args[]) throws ConfigurationException, FileNotFoundException {
        File file = new File(args[0] + ".properties");

        PropertiesConfiguration config = new PropertiesConfiguration();
        PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(config);
        layout.load(new InputStreamReader(new FileInputStream(file)));

        config.setProperty("test", "testValue");
        layout.save(new FileWriter("path\to\properties\file.properties", false));
    }
}

See also:

也可以看看:

回答by John Rix

The sample code for using the Apache Commons Configuration library contributed by Patrick Boos is unnecessarily complicated. You don't need to use PropertiesConfigurationLayout explicitly unless you require some advanced control over the output. PropertiesConfiguration by itself is sufficient to preserve comments and formatting:

由 Patrick Boos 提供的使用 Apache Commons Configuration 库的示例代码不必要地复杂。除非您需要对输出进行一些高级控制,否则您不需要显式使用 PropertiesConfigurationLayout。PropertiesConfiguration 本身足以保留注释和格式:

PropertiesConfiguration config = new PropertiesConfiguration("myprops.properties");
config.setProperty("Foo", "Bar");
config.save();

(Note: This code works for the existing 1.10 stable version. I have not checked if it works on the 2.0 alpha builds currently available.)

(注意:此代码适用于现有的 1.10 稳定版本。我没有检查它是否适用于当前可用的 2.0 alpha 版本。)

回答by Romain Linsolas

You can have a look to the Apache Commons Configuration, that contains PropertiesConfigurationclass. However, as I have never used it, I don't know if it preserves the comments and formatting...

您可以查看包含PropertiesConfiguration类的Apache Commons Configuration。但是,由于我从未使用过它,我不知道它是否保留了注释和格式...

However, it worthes a try...

不过,值得一试……

回答by Kevin Le

The configuration2 class have different syntax. Here is an example using them:

configuration2 类有不同的语法。这是使用它们的示例:

import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.PropertiesConfigurationLayout;

public void test() {
    PropertiesConfiguration config = new PropertiesConfiguration();
    PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout();
    config.setLayout(layout);
    layout.load(config, new FileReader("config.properties"));

    config.setProperty("KEY", "VALUE");
    StringWriter stringWriter = new StringWriter();
    layout.save(config, stringWriter);
    LOG.debug("Properties:\n{}", stringWriter.toString());
}

回答by Tarun Gupta

    File file = new File("src/test/resources/1automation.properties");
    PropertiesConfiguration config = new PropertiesConfiguration();
    PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(config);
    layout.load(new InputStreamReader(new FileInputStream(file)));
    FileWriter fw = new FileWriter("src/test/resources/1automation.properties",false);
    config.setProperty("myssi.admin.name", "testValue");
    layout.save(fw);

回答by Jeremy

The best answer contains a small mistake: The line:

最佳答案包含一个小错误:该行:

PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(config);

Must be replaced by:

必须替换为:

PropertiesConfigurationLayout layout = config.getLayout();

回答by Aaron Digulla

I once saw a class to do this with INI files but can't find the link anymore. If you can't find anything else, you can try DecentXML. I wrote this XML parser with the specific design goal to preserve the original formatting 100% (i.e. with comments, weird spaces in elements or around the root element, everything).

我曾经看到一个类可以使用 INI 文件执行此操作,但再也找不到链接了。如果您找不到其他任何东西,您可以尝试DecentXML。我写这个 XML 解析器的特定设计目标是 100% 保留原始格式(即带有注释、元素中的奇怪空格或根元素周围的所有内容)。

During parsing the resulting XML document, you just have to remember the elements which contain the value for your options and replace the text node in them. When you save, nothing untouched will change in any way.

在解析生成的 XML 文档期间,您只需记住包含选项值的元素并替换其中的文本节点。当您保存时,任何未受影响的东西都不会以任何方式改变。