【发布时间】:2010-10-08 15:11:01
【问题描述】:
虽然java.util.properties 允许读取和写入属性文件,但写入不会保留格式。不足为奇,因为它不绑定到属性文件。
是否有一个 PropertyFile 类 - 或类似的 - 保留 cmets 和空行并在适当位置更新属性值?
【问题讨论】:
标签: java properties
虽然java.util.properties 允许读取和写入属性文件,但写入不会保留格式。不足为奇,因为它不绑定到属性文件。
是否有一个 PropertyFile 类 - 或类似的 - 保留 cmets 和空行并在适当位置更新属性值?
【问题讨论】:
标签: java properties
只是为了回应 Jince Martin,他在写入的属性文件中遇到了尾随空格的问题,例如key = value 而不是 key=value。
你可以调用 layout.setGlobalSeparator("=") 来解决这个问题。
【讨论】:
最佳答案包含一个小错误: 行:
PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(config);
必须替换为:
PropertiesConfigurationLayout layout = config.getLayout();
【讨论】:
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());
}
【讨论】:
它并没有比 Apache 的 Commons Configuration API 好多少。这提供了从属性文件、XML、JNDI、JDBC 数据源等进行配置的统一方法。
它对属性文件的处理非常好。它允许您从您的属性生成一个PropertiesConfigurationLayout 对象,该对象尽可能多地保留有关您的属性文件的信息(空格、cmets 等)。当您保存对属性文件的更改时,这些更改将尽可能保留。
示例代码:
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));
}
}
另见:
【讨论】:
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);
【讨论】:
trim() 删除不需要的空格。
Patrick Boos 提供的使用 Apache Commons 配置库的示例代码过于复杂。除非您需要对输出进行一些高级控制,否则您不需要显式使用 PropertiesConfigurationLayout。 PropertiesConfiguration 本身就足以保留 cmets 和格式:
PropertiesConfiguration config = new PropertiesConfiguration("myprops.properties");
config.setProperty("Foo", "Bar");
config.save();
(注意:此代码适用于现有的 1.10 稳定版本。我没有检查它是否适用于当前可用的 2.0 alpha 版本。)
【讨论】:
save() ... :(
我曾经看到一个使用 INI 文件执行此操作的类,但再也找不到链接了。如果找不到其他内容,可以尝试DecentXML。我编写了这个 XML 解析器,其特定设计目标是 100% 保留原始格式(即使用 cmets、元素中或根元素周围的奇怪空格等)。
在解析生成的 XML 文档期间,您只需记住包含选项值的元素并替换其中的文本节点。保存后,任何未改动的内容都不会发生任何变化。
【讨论】:
您可以查看包含PropertiesConfiguration 类的Apache Commons Configuration。 不过因为没用过,不知道是不是保留了cmets和格式……
不过,值得一试……
【讨论】: