java - 属性文件未反射(reflect)使用 Apache Commons Configuration 修改的更改

标签 java properties apache-commons dynamic-programming apache-config

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

我为此编写了一个演示代码。

代码片段

    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();
            }
        }

    }

虽然当我多次运行代码时,可以正确显示属性的更新值,但在属性文件中看不到更改。

我尝试刷新整个工作区和项目,但属性文件仍然显示上一个条目,而此代码在控制台中显示更新的条目。

为什么我的属性文件没有更新?

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 应用程序包内

Dem1

名字为

Prop1.prop

我想从另一个类中读取这个属性文件

Reading.java

位于另一个包内

Dem2

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

即使在简单文件(主函数)中使用绝对路径后,它也不会反射(reflect)同一文件中的更改,而是在新文件中更新它。

我犯了一个很小的错误,但有人可以帮忙吗?

使用绝对路径,我也无法在正常的主方法中对同一属性文件进行更改。请提出建议。

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

最佳答案

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

绝对路径可以通过查询类路径上的资源来获取。类似于以下内容:

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

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

关于java - 属性文件未反射(reflect)使用 Apache Commons Configuration 修改的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16457474/

相关文章:

java - Apache Commons DiffBuilder

java - 从 Java 中的 WSDL 导入 Web 服务客户端

c# - 我可以修改 setting.setting 文件中的值吗?

maven - 将属性中的 WSDL URL 添加到 pom 中

java - splitByWholeSeparatorPreserveAllTokens 和 split 之间的区别

java - 在java中创建4​​字节唯一ID的策略

java - 类路径和命令行开关(-cp、-D)

java - 从 ListView 启动新 Activity

基于Java split的精确正则表达式匹配

objective-c - Objective C - 使用相同实例变量的两个合成属性?