diff kopieckreta/src/ConfigFile.java @ 0:2787f5e749ae

INIT
author prymula <prymula76@outlook.com>
date Thu, 21 Sep 2023 22:33:57 +0200
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kopieckreta/src/ConfigFile.java	Thu Sep 21 22:33:57 2023 +0200
@@ -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;
+    }
+    
+}