gpt4 book ai didi

java - Apache Commons 配置 : read a . 属性文件并重写它而不做任何更改

转载 作者:行者123 更新时间:2023-11-30 03:14:26 25 4
gpt4 key购买 nike

由于我不知道更好的解决方案,我目前正在编写小型Java类来处理.properties文件以合并它们,删除重复的属性,覆盖属性等(我需要处理许多文件和大量的文件)属性)。

org.apache.commons.configuration.PropertiesConfiguration 非常适合读取属性文件(使用 org.apache.commons.configuration.AbstractFileConfiguration.load(InputStream, String),但是如果我使用 org.apache.commons 重写该文件.configuration.AbstractFileConfiguration.save(File),我有两个问题:

  1. 原始布局和注释丢失。我将尝试 PropertiesConfigurationLayout,它应该在这里有所帮助(请参阅 How to overwrite one property in .properties without overwriting the whole file? )并发布结果
  2. 属性略有修改。重音 é 和 è 被重写为 unicode 字符 (\u00E9),这是我不想要的。 Afaik .properties 文件通常是 ISO-8859-1 (我认为我的是),因此不需要转义。在调用 org.apache.commons.configuration.AbstractFileConfiguration.load(InputStream, String) 时指定编码并没有什么区别,因为当未指定时,默认情况下无论如何都会使用相同的编码(private static final String DEFAULT_ENCODING = "ISO-8859-1”;)。我能做些什么呢?

最佳答案

进行一些测试,我认为您可以使用 CombinedConfiguration 加上 OverrideCombiner 来完成您想要的操作。基本上,属性将自动合并,布局的技巧是从加载的文件之一获取布局:

CombinedConfiguration props = new CombinedConfiguration();

final PropertiesConfiguration defaultsProps = new PropertiesConfiguration(new File("/tmp/default.properties"));
final PropertiesConfiguration customProps = new PropertiesConfiguration(new File("/tmp/custom.properties"));
props.setNodeCombiner(new OverrideCombiner());
props.addConfiguration(customProps); //first should be loaded the override values
props.addConfiguration(defaultsProps); // last your 'default' values

PropertiesConfiguration finalFile = new PropertiesConfiguration();
finalFile.append(props);
PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(finalFile, defaultsProps.getLayout()); //here we copy the layout from the 'base file'
layout.save(new FileWriter(new File("/tmp/app.properties")));

编码问题我不知道是否可以找到解决方案。

关于java - Apache Commons 配置 : read a . 属性文件并重写它而不做任何更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32965587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com