Property file not reflecting the modified changes using Apache Commons Configuration

末鹿安然 提交于 2019-12-10 11:42:28

问题


I am trying to explore Apache commons configuration to dynamically load the property file and do modification in the file and save it.

I wrote a demo code for the same.

Code Snippet

    package ABC;


    import org.apache.commons.configuration.ConfigurationException;
    import org.apache.commons.configuration.PropertiesConfiguration;
    import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;




    public class Prop {

        public static void main(String[] args)
        {

            try {
URL propertiesURL = Prop.class.getResource("/d1.properties");

            if (propertiesURL == null) {
              System.out.println("null");
            }
String absolutePath=propertiesURL.getPath();
                PropertiesConfiguration pc = new PropertiesConfiguration(absolutePath);
                pc.setReloadingStrategy(new FileChangedReloadingStrategy());
                String s=(String)pc.getProperty("key_account_sales");
                System.out.println("s is " +  s);
                pc.setAutoSave(true);
                pc.setProperty("key_account_sales", "Dummy");
                pc.save();
                System.out.println("Modified as well");
                String sa=(String)pc.getProperty("key_account_sales");

                System.out.println("s is " +  sa);
            }catch(ConfigurationException ce)
            {
                ce.printStackTrace();
            }
        }

    }

Although when I run the code multiple times, the updated value for the property is being properly shown but the changes are not seen in the Property file.

I tried refreshing the entire workspace and the project but still the property file shows the previous entry whereas this code displays the updated entry in console.

Why my property file is not getting updated?

Well I noticed that a new file with same name was formed inside bin directory of my IDE workspace. This new file contains the required changes.

However I still want that the old file should be updated with the new value and instead of creating a new file, it should update in the old file itself.

My property file is located inside a Web Application package say

Dem1

by the name of

Prop1.prop

I want to read this property file from in another class say

Reading.java

located inside another package

Dem2

, do changes in this same property file and show it to another user. It is a web application being deployed on an application server.

Even after using the absolute path in a simple file (main function) it is not reflecting the changes in the same file but updating it in new file.

I am doing a very slight mistake but can someone please help.

Using absolute path I am not able to make changes in the same property file in normal main method also. Please suggest.

New file in bin directory is created instead of updating the same file in src folder.


回答1:


You should be able to solve this using absolute paths. The PropertiesConfiguration class is finding your properties file somewhere on the classpath and only knows to write back to "d1.properties"; hence you have a file appearing in your bin directory.

The absolute path can be obtained by querying resources on the classpath. Something like the following:

URL propertiesURL = Prop.class.getResource("/d1.properties");
if (propertiesURL == null) {
  // uh-oh...
}

String absolutePath = propertiesURL.getPath();
// Now use absolutePath


来源:https://stackoverflow.com/questions/16457474/property-file-not-reflecting-the-modified-changes-using-apache-commons-configura

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