From c52dbcd71658dfc5b3ec2c709e838e7cc5167075 Mon Sep 17 00:00:00 2001 From: Lauri Maaninka Date: Mon, 11 May 2026 02:46:42 +0300 Subject: [PATCH] =?UTF-8?q?Otettu=20Path.of()=20k=C3=A4ytt=C3=B6=C3=B6n=20?= =?UTF-8?q?tiedostojen=20luvussa=20(k=C3=A4ytt=C3=B6j=C3=A4rjestelm=C3=A4?= =?UTF-8?q?=20riippumattomuus)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/sqldialects.xml | 6 ---- src/main/java/fi/lpam/Main.java | 20 +++++++++++-- .../java/fi/lpam/dataluokat/Asetukset.java | 29 +++++++++++++++++++ .../java/fi/lpam/dataluokat/Tietokanta.java | 12 ++++---- src/main/resources/app.properties | 1 - src/main/resources/defaults.properties | 3 ++ 6 files changed, 56 insertions(+), 15 deletions(-) delete mode 100644 .idea/sqldialects.xml create mode 100644 src/main/java/fi/lpam/dataluokat/Asetukset.java delete mode 100644 src/main/resources/app.properties create mode 100644 src/main/resources/defaults.properties diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml deleted file mode 100644 index c0e01ca..0000000 --- a/.idea/sqldialects.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/src/main/java/fi/lpam/Main.java b/src/main/java/fi/lpam/Main.java index 82c080a..17b61c7 100644 --- a/src/main/java/fi/lpam/Main.java +++ b/src/main/java/fi/lpam/Main.java @@ -1,12 +1,17 @@ package fi.lpam; -import java.io.IOException; +import org.apache.commons.lang3.SystemUtils; + +import java.io.*; +import java.nio.file.Path; import java.util.Properties; public class Main { public static boolean dev; public static Properties properties; + public static final File asetustiedosto = Path.of(SystemUtils.getUserHome().getAbsolutePath(), "OneDrive", "Kuljetusruokasovellus", "asetukset.properties").toFile(); + static void main(String[] args) { //noinspection RedundantIfStatement if (args.length > 0) { @@ -17,13 +22,24 @@ public class Main { try { properties = new Properties(); - properties.load(Main.class.getClassLoader().getResourceAsStream("app.properties")); + properties.load(Main.class.getClassLoader().getResourceAsStream("defaults.properties")); + properties.load(new FileInputStream(asetustiedosto)); } catch (IOException e) { //noinspection CallToPrintStackTrace e.printStackTrace(); } + properties.forEach((key, value) -> { + System.out.println(key + ": " + value); + }); + App.main(); + + try (OutputStream os = new FileOutputStream(asetustiedosto)) { + properties.store(os, ""); + } catch (IOException e) { + throw new RuntimeException(e); + } } } \ No newline at end of file diff --git a/src/main/java/fi/lpam/dataluokat/Asetukset.java b/src/main/java/fi/lpam/dataluokat/Asetukset.java new file mode 100644 index 0000000..e5b0f96 --- /dev/null +++ b/src/main/java/fi/lpam/dataluokat/Asetukset.java @@ -0,0 +1,29 @@ +package fi.lpam.dataluokat; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +public class Asetukset extends Properties { + private static Asetukset asetukset; + + private Asetukset() { + super(); + asetukset = this; + System.out.println("Luotu asetukset"); + try (InputStream is = new FileInputStream("defaults.properties")) { + asetukset.load(is); + asetukset.forEach((k, v) -> { + System.out.println(k + ": " + v); + }); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public Properties get() { + if (asetukset == null) {new Properties();} + return asetukset; + } +} diff --git a/src/main/java/fi/lpam/dataluokat/Tietokanta.java b/src/main/java/fi/lpam/dataluokat/Tietokanta.java index 8603e3b..d41f4ed 100644 --- a/src/main/java/fi/lpam/dataluokat/Tietokanta.java +++ b/src/main/java/fi/lpam/dataluokat/Tietokanta.java @@ -6,23 +6,23 @@ import fi.lpam.Main; import org.apache.commons.lang3.SystemUtils; import java.io.*; +import java.nio.file.Path; import java.sql.*; import java.util.Objects; public class Tietokanta { private static String osoite; - private static String dbPath = SystemUtils.getUserHome().getAbsolutePath() + "\\OneDrive\\Kuljetusruokasovellus\\tietokanta.db"; + private static File tietokantatiedosto = Path.of(SystemUtils.getUserHome().getAbsolutePath(), "OneDrive", "Kuljetusruokasovellus", "tietokanta.db").toFile(); public Tietokanta(boolean dev) { - if (dev) dbPath = "testikanta.db"; - File file = new File(dbPath); + if (dev) tietokantatiedosto = new File("testikanta.db"); try { - boolean _ = file.getParentFile().mkdirs(); - boolean _ = file.createNewFile(); + boolean _ = tietokantatiedosto.getParentFile().mkdirs(); + boolean _ = tietokantatiedosto.createNewFile(); } catch (Exception _) {} - osoite = "jdbc:sqlite:" + file.getAbsolutePath(); + osoite = "jdbc:sqlite:" + tietokantatiedosto.getAbsolutePath(); try (Connection conn = haeYhteys()) { diff --git a/src/main/resources/app.properties b/src/main/resources/app.properties deleted file mode 100644 index e5683df..0000000 --- a/src/main/resources/app.properties +++ /dev/null @@ -1 +0,0 @@ -version=${project.version} \ No newline at end of file diff --git a/src/main/resources/defaults.properties b/src/main/resources/defaults.properties new file mode 100644 index 0000000..84a1be6 --- /dev/null +++ b/src/main/resources/defaults.properties @@ -0,0 +1,3 @@ +version=${project.version} + +viimeisinVersio=0.0.0 \ No newline at end of file