From d8afc9f5c2aa97a96ca326c0178a7fc9577afaaf Mon Sep 17 00:00:00 2001 From: Przemyslaw Date: Mon, 1 Apr 2024 09:02:22 +0200 Subject: Init --- src/ConfigFile.java | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/ConfigFile.java (limited to 'src/ConfigFile.java') diff --git a/src/ConfigFile.java b/src/ConfigFile.java new file mode 100644 index 0000000..b3c380d --- /dev/null +++ b/src/ConfigFile.java @@ -0,0 +1,96 @@ + +package src; + +/** + * + * @author przem + */ + +import java.io.FileWriter; +import java.io.File; +import java.io.IOException; +import java.io.FileNotFoundException; // Import this class to handle errors +import java.util.Scanner; +import java.util.*; + +public class ConfigFile { + public String userDir="", userHome="", osName="", osVersion="", jvmVersion=""; + String name = "/kopieckreta.txt"; + public ConfigFile() throws IOException{ + for (Map.Entry e : System.getProperties().entrySet()) { + //System.out.println(String.format("%s = %s", e.getKey(), e.getValue())); + if (e.getKey().equals("user.dir")) { + userDir=String.format("%s", e.getValue()); + } + if (e.getKey().equals("user.home")) { + userHome=String.format("%s", e.getValue()); + + } + if (e.getKey().equals("os.name")) { + osName=String.format("%s", e.getValue()); + + } + if (e.getKey().equals("os.version")) { + osVersion=String.format("%s", e.getValue()); + + } + if (e.getKey().equals("java.vm.version")) { + jvmVersion=String.format("%s", e.getValue()); + + } + } + + + File file=new File (userHome+name); + if (file.createNewFile()){ + System.out.println("Plik zostal utworzony"); + try { + FileWriter myWriter = new FileWriter(userHome+name); + myWriter.write("1"); + myWriter.close(); + System.out.println("Successfully wrote to the file."); + } catch (IOException e) { + System.out.println("An error occurred."); + e.printStackTrace(); + } + } + else { + System.out.println("Plik istnieje"); + } + } + public void save(boolean b){ + String s=""; + try { + FileWriter myWriter = new FileWriter(userHome+name); + if (b) s="1"; + else s="0"; + myWriter.write(s); + myWriter.close(); + System.out.println("Successfully wrote to the file."); + } catch (IOException e) { + System.out.println("An error occurred."); + e.printStackTrace(); + } + } + public boolean load() { + String data = ""; + boolean r =false; + try { + File myObj = new File(userHome+name); + Scanner myReader = new Scanner(myObj); + while (myReader.hasNextLine()) { + data = myReader.nextLine(); + System.out.println(data); + } + myReader.close(); + } catch (FileNotFoundException e) { + System.out.println("An error occurred."); + e.printStackTrace(); + } + + if (data.equals("1")) r=true; + + return r; + } + +} -- cgit v1.2.3