Ich wollte, dass die Einstellungen beim Start geladen werden und die neuen Einstellungen aus den GUI-Eingaben geschrieben werden, wenn Sie auf „OK“ klicken SettingsDialogPanel und nach dem Neustart lädt die Anwendung die auf der Festplatte gespeicherten Einstellungen.
Die Einstellungseigenschaftendatei befindet sich in src/main/resources/settings.properties.
I Dachte, dass es Funktioniert nicht, da es sich im Ordner „Ressourcen“ in meiner Maven-Anwendung befindet, aber eine andere Datei „tasks.dat“ schreibt und liest normalerweise am selben Ort.
Code: Select all
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);
});
}
}
Code: Select all
package org.app.settings;
import java.util.Properties;
public interface PanelWithSettings {
Properties getSettings();
void setSettings(Properties s);
}