From cc0c58070c1bc6ab918890e65b16ba936ec0bb67 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 17 Mar 2018 19:43:06 +0700 Subject: [PATCH] Store user preferences to a XML file just in case system preferences subsystem (e.g. Windows Registry) has issues --- source/net/filebot/Main.java | 3 +++ source/net/filebot/Settings.java | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/source/net/filebot/Main.java b/source/net/filebot/Main.java index 46979f47..62519260 100644 --- a/source/net/filebot/Main.java +++ b/source/net/filebot/Main.java @@ -194,6 +194,9 @@ public class Main { HistorySpooler.getInstance().commit(); SupportDialog.maybeShow(); + // backup preferences on exit + Settings.store(ApplicationFolder.AppData.resolve("preferences.backup.xml")); + System.exit(0); })); diff --git a/source/net/filebot/Settings.java b/source/net/filebot/Settings.java index b734de3a..9f736f7c 100644 --- a/source/net/filebot/Settings.java +++ b/source/net/filebot/Settings.java @@ -2,6 +2,13 @@ package net.filebot; import static net.filebot.Logging.*; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; import java.util.Locale; import java.util.ResourceBundle; import java.util.logging.Level; @@ -249,4 +256,20 @@ public final class Settings { } } + public static void store(File f) { + try (OutputStream out = new BufferedOutputStream(new FileOutputStream(f))) { + Preferences.userRoot().exportSubtree(out); + } catch (Exception e) { + debug.log(Level.SEVERE, e, e::toString); + } + } + + public static void restore(File f) { + try (InputStream in = new BufferedInputStream(new FileInputStream(f))) { + Preferences.importPreferences(in); + } catch (Exception e) { + debug.log(Level.SEVERE, e, e::toString); + } + } + }