apache-commons-config PropertiesConfiguration:最后一个属性丢失后的注释

发布于 2024-11-15 04:44:33 字数 1270 浏览 2 评论 0原文

我正在使用 PropertiesConfiguration 编辑属性文件。这让我可以保留评论。除了最后一个键后面的注释之外,一切正常。

例如输入文件

# *** A comment
GameCheck.no=No
**#  end coment**

输出如下。它丢失了最后一个键之后的注释

# *** A comment
GameCheck.no = myvar

代码如下。

package trials;

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

import java.io.FileWriter;
import java.io.IOException;

public class EditVersion {

    public static void main(String[] args) {

        try {
            PropertiesConfiguration config =  new PropertiesConfiguration("C:\\try\\in.properties");
            config.setProperty("application.version", "myvar");
            PropertiesConfigurationLayout layout = config.getLayout();

            config.save( new FileWriter( "c:/try/out.props"));
        } catch (ConfigurationException e) {

        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}

解决方法是在文件末尾添加一个虚拟属性。有正确的方法吗?

I am using PropertiesConfiguration to edit property file. This allows me to retain comments. All works fine except for comments that comes after the last key..

For example input file

# *** A comment
GameCheck.no=No
**#  end coment**

The output is as below. It lost comment that was after last key

# *** A comment
GameCheck.no = myvar

The code as below.

package trials;

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

import java.io.FileWriter;
import java.io.IOException;

public class EditVersion {

    public static void main(String[] args) {

        try {
            PropertiesConfiguration config =  new PropertiesConfiguration("C:\\try\\in.properties");
            config.setProperty("application.version", "myvar");
            PropertiesConfigurationLayout layout = config.getLayout();

            config.save( new FileWriter( "c:/try/out.props"));
        } catch (ConfigurationException e) {

        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}

Work around is to add a dummy property towards the end of file. Is there a correct way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

五里雾 2024-11-22 04:44:33

这是一个应该在项目的 JIRA 中报告的错误:)

https://issues.apache.org /jira/browse/配置

This is a bug that should be reported in the project's JIRA :)

https://issues.apache.org/jira/browse/CONFIGURATION

韬韬不绝 2024-11-22 04:44:33

您可以尝试从 **# end comment** 开头删除 ** 以防产生影响。

还要检查一下如果最后一行后面有一个空行是否有帮助。

You could try to remove the ** from the beginning of **# end coment** in case it makes a difference.

Also check out whether it helps if there is an empty line just after your last actual line.

吖咩 2024-11-22 04:44:33

这似乎是旧版本的 commons-configuration ( 1.8 <= ) 的一个错误,

版本 1.10 应该可以解决这个问题。

从这里下载 https://commons.apache.org/proper/commons-configuration /download_configuration.cgi

This seems to be a bug for old versions of commons-configuration ( 1.8 <= )

Version 1.10 should solve the issue.

Download from here https://commons.apache.org/proper/commons-configuration/download_configuration.cgi

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文