apache-commons-config PropertiesConfiguration: comments after last property is lost

╄→гoц情女王★ 提交于 2019-12-07 06:13:37

问题


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?


回答1:


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

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




回答2:


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.



来源:https://stackoverflow.com/questions/6347749/apache-commons-config-propertiesconfiguration-comments-after-last-property-is-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!