Updating resource files at runtime

倖福魔咒の 提交于 2020-07-22 06:03:50

问题


When my application starts it reads a configuration properties file using the following code:

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

I am able to read and set individual properties.

The properties file is located in src/main/resources and after I build the application using maven, a copy of it is placed in target/classes. The jar file that is created also has a copy of it in the root directory when I open it up.

I would also like to be able to overwrite the properties file so that next time the application starts up, then it will read the new updated file. How do I achieve this? Is it even possible?

I found this question but no answers.

I've tried this:

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

which works if I just want to create a new file altogether. I would then have to modify the application so that it reads from a given folder rather than what originated from the resources folder. Is this what I should be doing?

I'm fairly new to Java so please go easy.


回答1:


Storing the initial, default properties in the jar file, as resources is fine.

But if you want them to be writable, then you need to really store them as a file somewhere on the disk. Typically, under a .yourapp directory (or in a .yourapp file) inside the user's home directory.

So, try finding the file, and if not present, fallback to the resources. When writing, always write to the file.




回答2:


This is an example code you can use for this. You create a config folder in the project root directory, an inside it you place your app.properties file

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

}



来源:https://stackoverflow.com/questions/44889793/updating-resource-files-at-runtime

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