-Properties不使用FileInputStream写入?

问题描述 投票:0回答:1

I使用Java制作了一个简单的待办事项列表应用程序。这是一个小项目。它使用settingsStorage类来加载和写入文件设置。该程序通常会在启动上加载默认设置,如果我更改文件,例如darktheme = false,它将启动并将默认颜色设置为点亮。如果我还更改将正常加载的宽度属性,则应用文件的宽度和高度。

问题是,当我将设置写入属性对象时,然后将其传递到settingsStorage.WriteProperties(CurrentSettings)时,似乎在写下它,因为在我重新加载属性后,它在将方法传递给了该方法时,写。但是,当我重新启动应用程序时,文件没有更改,并且程序加载了现有数据。我使用设置索引加载并编写设置,因此文件始终相同。

i我希望它加载启动的设置,在settingsDialogPanel中按OK时,从GUI输入中编写新设置,然后在重新启动应用程序后,它加载了存储在磁盘上的设置。

设置属性文件位于src/main/resources/settings.properties中。我当时认为它不起作用,因为它在我的Maven应用程序内的资源文件夹上,但是另一个文件任务。DAT在同一位置上写下并读取正常读取。

package org.app.settings; import java.io.*; import java.util.Properties; public class SettingsStorage { static final String SETTINGS_PATH = "settings.properties"; PanelWithSettings mp; public SettingsStorage(PanelWithSettings mp) { this.mp = mp; } public void loadSettings () { try { InputStream is = getClass().getClassLoader().getResourceAsStream(SETTINGS_PATH); if (is==null) { System.out.println("failed to access file, creating a new one!"); return; } Properties p = new Properties(); p.load(is); mp.setSettings(p); is.close(); } catch (FileNotFoundException e) { System.out.println("error: settingsstorage::loadsettings: file "+ SETTINGS_PATH+" not found, creating new file..."); createFile(); } catch (IOException e) { System.out.println("settingsstorage.loadsettings: something went wrong: "+e); } } public static void writeSettings (Properties s) { try { FileOutputStream fos = new FileOutputStream(SETTINGS_PATH); s.store(fos, "settings"); fos.close(); System.out.println("Given properties wrote successfully"); } catch (IOException e) { System.out.println("settingsstorage::writesettings(Properties): something went wrong: "+e); } } public void writeSettings () { FileOutputStream fos; try { fos = new FileOutputStream(SETTINGS_PATH); Properties p = mp.getSettings(); p.store(fos, "settings"); fos.close(); System.out.println("Panel's Properties wrote successfully"); } catch (FileNotFoundException e) { System.out.println("settingsstorage::writesettings: file "+ SETTINGS_PATH+" not found, creating new file..."); createFile(); } catch (IOException e) { System.out.println("settingsstorage::writesettings: something went wrong: "+e); } } public static Properties loadPropertiesFromDisk () { try { FileInputStream fis = new FileInputStream(SETTINGS_PATH); Properties p = new Properties(); p.load(fis); fis.close(); return p; } catch (FileNotFoundException e) { System.out.println("error: settingsstorage::loadsettings: file "+ SETTINGS_PATH+" not found, creating new file..."); System.err.println("Cannot access "+SETTINGS_PATH.substring('.')+ " , FileNotFoundException (file not found)"); } catch (IOException e) { System.out.println("settingsstorage.loadsettings: something went wrong: "+e); } return null; // failed } public void createFile () { File f = new File(SETTINGS_PATH); f.delete(); try { f.createNewFile(); } catch (IOException e) { System.out.println("settingsstorage::createfile: something went wrong: "+e); } } public static void main (String[] args) { Properties p = loadPropertiesFromDisk(); p.forEach((key, val) -> { System.out.println(key+" => "+val); }); System.out.println("Write settings"); p.clear(); p.put("darkTheme", "false"); p.put("width", "599"); p.put("height", "699"); writeSettings(p); Properties properties = loadPropertiesFromDisk(); properties.forEach((key, val) -> { System.out.println(key+" => "+val); }); } }

package org.app.settings; import java.util.Properties; public interface PanelWithSettings { Properties getSettings(); void setSettings(Properties s); }

	
this:

java maven file r.java-file
1个回答
5
投票

有点错(这是
GetMyClass.class.getResourceAsStream("/" + SETTINGS_PATH);
-这是做到这一点的标准方法,并且以严格的方式工作比您的“ take'”更重要,但更重要的是,根本不是正确的呼吁。那是用于更改cannot的资源 - 因此不是设置文件。它看起来位于您的类的同一位置,因此,在
jar
文件或应用程序的安装dir中,它是不正确的(除非您的操作系统被错误配置并允许应用程序写入自己的安装空间,否则某些OSS(尤其是Windows) ,允许,但这是一个坏主意)。 因此,您应该完全重写应用程序的这一部分。正确的方法是:

upon启动您加载设置文件。您可以将其加载new FileInputStream(LOCATION)
,位置可能是
System.getProperty("user.dir") + "/myapp.settings")

,即将其存储在用户的家中,而不是应用程序安装dir(因为您不会,或者不应该不应该写入访问权限)。

    如果它不存在,并且只有在不存在时,您就可以回到模板过程:使用
  • Template

    设置文件的应用程序船。这是其静态,不变的文件的一部分,就像您的类文件一样:将应用程序作为JAR打包时,它在其中,是您的源分发的一部分,您可以使用

    MyClass.class.getResource("/settings.template.file")
    来获取。

    您将模板复制到“实际”设置文件位置。
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.