简体   繁体   English

在Maven项目中保存用户状态

[英]save user status in maven project

I am writing a javaFX project with netbeans IDE and maven. 我正在用netbeans IDE和maven编写一个javaFX项目。

I am saving user edits in a property file and i loading it when the application start up and i want to update it when application shuts down to reuse it next time. 我将用户编辑保存在属性文件中,并在应用程序启动时加载它,我想在应用程序关闭时更新它,以备下次使用。

So I am reading the property file as below: 所以我正在读取属性文件,如下所示:

public static Properties propConfig = new Properties();

InputStream input;
input = Config.class.getClassLoader().getResourceAsStream("config/displayConfig.properties");
propConfig.load(input);

which works fine.. 效果很好..

but i don't know how to update the property file :( as 但我不知道如何更新属性文件:(作为

    output =  new FileOutputStream( new File(Config.class.getClassLoader().getResource("config/displayConfig.properties").toURI()) );

is not working since it reads the resource file from jar 无法工作,因为它从jar读取资源文件

jar:file:/D:/freelance%20projects/01%20school%20tool%20bar/mavenprojectFX/target/racer40-1.0-SNAPSHOT.jar!/config/displayConfig.properties

Normally it's not recommended to update files in JARs. 通常,不建议在JAR文件中更新文件。 Use local files instead, eg: 请改用本地文件,例如:

Path userDirPath = Paths.get(System.getProperty("user.home"), ".<myAppSymbolicName>", "<myAppVersion>");
Path configDirPath =  userDirPath.resolve("config"); 
Path displayConfigFilePath = configDirPath.resolve("displayConfig.properties");
// read file

well i have done a workaround, since I can only read files from jar i have defined a new properties file and i have defined the configuration path in it. 好吧,我做了一个变通办法,因为我只能从jar中读取文件,所以我定义了一个新的属性文件,并且在其中定义了配置路径。

i have modified resources in POM.xml to set filtering true 我已经在POM.xml中修改了资源以将过滤设置为true

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>

i have placed the path property file in "src/main/resources/config" folder 我已经将路径属性文件放在“ src / main / resources / config”文件夹中

the path file contains 路径文件包含

Path=${project.build.directory}

and i am reading it as the following 我正在阅读以下内容

InputStream pathStream
                = Config.class.getClassLoader().getResourceAsStream("config/path.properties");

        Properties pathProperties = new Properties();
        pathProperties.load(pathStream);
        path = pathProperties.get("Path").toString().replace("target", "");
        System.out.println(pathProperties.get("Path"));
        pathStream.close();

then i am using 'path' variable as the following 然后我正在使用'path'变量,如下所示

OutputStream  output = new FileOutputStream(new File(path + displayConfig.properties"));

so I am now putting the configuration file "displayConfig.properties" in the same path of jar. 所以我现在将配置文件“ displayConfig.properties”放在jar的相同路径中。

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

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