属性文件中的更改不会保存在 JAR 文件中

Changes in the properties file are not saved inside the JAR file

我的 Java 程序中有一个存储多个值的属性文件。在设置 window 中,用户可以编辑这些值并保存修改以供下次 运行 程序使用。

这是从属性文件加载属性的代码:

public class AppProperties {
    private final static AppProperties appProperties = new AppProperties();
    private static Properties properties;
    private final static String preferencesSourcePath = "/res/pref/Properties.properties";

    private AppProperties() {
        properties = new Properties();

        try {
            properties.load(getClass().getResourceAsStream(preferencesSourcePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

在这里,在属性文件中保存属性的方法(在同一个class):

    public static void saveAppPropertiesFile() {
        try {
            OutputStream outputStream = new FileOutputStream(new File(AppProperties.class.getResource(preferencesSourcePath).getPath()));
            properties.store(outputStream, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我已经尝试过这个功能,当我在我的 IDE 中时它做得很好,但当我 运行 JAR 文件时它不起作用。事实上,它适用于当前会话,但未保存在 JAR 文件中。

在控制台中,它显示:

java.io.FileNotFoundException: file:\C:\Users\HP\HEIG\EcoSimSOO\out\artifacts\EcoSimSOO_jar\EcoSimSOO.jar!\res\pref\Properties.properties (La syntaxe du nom de fichier, de répertoire ou de volume est incorrecte)
    at ...
    at res.pref.AppProperties.saveAppPropertiesFile(AppProperties.java:31)

这是我尝试这样做的地方:

AppProperties.class.getResource(preferencesSourcePath)

我已阅读 this post 但我不明白这个问题以及我必须做些什么来解决这个问题...

感谢您的帮助。

您不应向 JAR 文件写入任何内容。从技术上讲,JAR 下的资源是只读的。您不能 write/modify JAR 中的任何文件。

I have a properties file in my Java program that stores several values. In the settings window, the user can edit these values and save the modifications for the next time he runs the program.

您可以使用 database/cache/flat-file 来存储这些值并在 运行 时间内从中读取这些值,而不是将这些修改后的值保存在属性文件中。