【问题标题】:Updating resource files at runtime在运行时更新资源文件
【发布时间】:2017-12-06 23:19:12
【问题描述】:

当我的应用程序启动时,它会使用以下代码读取配置属性文件:

        Properties properties = new Properties();

        // parse the config resource

        try (InputStream input = getClass().getClassLoader().getResourceAsStream(filename))
        {
            if (input == null)
            {
                // throw exception
            }

            // read the property list (key-value pairs) from the input byte stream
            properties.load(input);
        }

我能够读取和设置个别属性。

属性文件位于src/main/resources,在我使用maven 构建应用程序后,它的副本放在target/classes。当我打开它时,创建的 jar 文件在根目录中也有一个副本。

我还希望能够覆盖属性文件,以便下次应用程序启动时,它将读取新的更新文件。我如何实现这一目标?甚至可能吗?

我找到了this 的问题,但没有答案。

我试过了:

        try (OutputStream output = new FileOutputStream(filename))
        {
            properties.store(output, null);
        }

如果我只想完全创建一个新文件,这很有效。然后我必须修改应用程序,以便它从给定文件夹中读取,而不是从资源文件夹中读取。这是我应该做的吗?

我是 Java 新手,所以请放轻松。

【问题讨论】:

    标签: java maven properties resources


    【解决方案1】:

    initial, default 属性存储在 jar 文件中,作为资源很好。

    但如果您希望它们是可写的,那么您需要将它们作为文件真正存储在磁盘上的某个位置。通常,在用户主目录内的.yourapp 目录下(或.yourapp 文件中)。

    因此,尝试查找文件,如果不存在,则回退到资源。写入时,始终写入文件。

    【讨论】:

    • 我建议检查java.util.prefs.Preferences 类,因为它允许将数据存储在适合操作系统的位置(Windows 注册表、.directroy、~/Library/Application Suppert)
    【解决方案2】:

    这是您可以使用的示例代码。您在项目根目录中创建一个配置文件夹,在其中放置您的 app.properties 文件

    package com.yourparckage;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class Config {
    
        /* Create basic object */
        private ClassLoader objClassLoader = null;
        private Properties commonProperties = new Properties();
        public static final String CONFIG_FILE = "config/app.properties";
    
        /**
         * This method loads config data from properties file
         * 
         */
        public Config() {
            objClassLoader = getClass().getClassLoader();
        }
    
        public String readKey(String propertiesFilename, String key) 
        {
            /* Simple validation */
            if (propertiesFilename != null && !propertiesFilename.trim().isEmpty() && key != null
                    && !key.trim().isEmpty()) {
                /* Create an object of FileInputStream */
                InputStream objFileInputStream = null;
    
                /**
                 * Following try-catch is used to support upto 1.6. Use try-with-resource in JDK
                 * 1.7 or above
                 */
                try {
                    /* Read file from resources folder */
                    objFileInputStream = new FileInputStream(propertiesFilename);
                    /* Load file into commonProperties */
                    commonProperties.load(objFileInputStream);
                    /* Get the value of key */
                    return String.valueOf(commonProperties.get(key));
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                } finally {
                    /* Close the resource */
                    if (objFileInputStream != null) {
                        try {
                            objFileInputStream.close();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            }
            return null;
        }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多