属性文件未使用Apache Commons Configuration反映修改的更改 [英] Property file not reflecting the modified changes using Apache Commons Configuration

查看:223
本文介绍了属性文件未使用Apache Commons Configuration反映修改的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试探索Apache commons配置以动态加载属性文件并在文件中进行修改并保存。

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.

代码片段

    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?


我注意到一个同名的新文件是在我的IDE工作区的bin
目录中形成。这个新文件包含所需的
更改。

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.

我的属性文件位于Web应用程序包中说

My property file is located inside a Web Application package say


Dem1

Dem1

名称of


Prop1.prop

Prop1.prop

I想要从另一个类中读取此属性文件说

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


Reading.java

Reading.java

位于另一个套餐内


Dem2

Dem2

,在同一属性文件中进行更改并将其显示给其他用户。它是一个部署在应用程序服务器上的Web应用程序。

, 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.

使用绝对路径我也无法在普通main方法中对同一属性文件进行更改。请建议。

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


创建bin目录中的新文件,而不是更新src文件夹中的同一文件

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


推荐答案

您应该能够使用绝对路径解决此问题。 PropertiesConfiguration 类在类路径的某处找到属性文件,只知道写回d1.properties ;因此,您的bin目录中会出现一个文件。

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

这篇关于属性文件未使用Apache Commons Configuration反映修改的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆