繁体   English   中英

在运行时更新资源文件

[英]Updating resource files at runtime

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

        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文件在根目录中也有一个副本。

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

我发现了这个问题,但没有答案。

我已经试过了:

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

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

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

由于资源还可以,因此将初始的默认属性存储在jar文件中。

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

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

这是您可以用于此的示例代码。 您在项目根目录中创建一个config文件夹,在其中放置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;
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM