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

INIT
author prymula <prymula76@outlook.com>
date Thu, 21 Sep 2023 22:33:57 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
prymula <prymula76@outlook.com>
parents:
diff changeset
1
prymula <prymula76@outlook.com>
parents:
diff changeset
2 package src;
prymula <prymula76@outlook.com>
parents:
diff changeset
3
prymula <prymula76@outlook.com>
parents:
diff changeset
4 /**
prymula <prymula76@outlook.com>
parents:
diff changeset
5 *
prymula <prymula76@outlook.com>
parents:
diff changeset
6 * @author przem
prymula <prymula76@outlook.com>
parents:
diff changeset
7 */
prymula <prymula76@outlook.com>
parents:
diff changeset
8
prymula <prymula76@outlook.com>
parents:
diff changeset
9 import java.io.FileWriter;
prymula <prymula76@outlook.com>
parents:
diff changeset
10 import java.io.File;
prymula <prymula76@outlook.com>
parents:
diff changeset
11 import java.io.IOException;
prymula <prymula76@outlook.com>
parents:
diff changeset
12 import java.io.FileNotFoundException; // Import this class to handle errors
prymula <prymula76@outlook.com>
parents:
diff changeset
13 import java.util.Scanner;
prymula <prymula76@outlook.com>
parents:
diff changeset
14 import java.util.*;
prymula <prymula76@outlook.com>
parents:
diff changeset
15
prymula <prymula76@outlook.com>
parents:
diff changeset
16 public class ConfigFile {
prymula <prymula76@outlook.com>
parents:
diff changeset
17 public String userDir="", userHome="", osName="", osVersion="", jvmVersion="";
prymula <prymula76@outlook.com>
parents:
diff changeset
18 String name = "/kopieckreta.txt";
prymula <prymula76@outlook.com>
parents:
diff changeset
19 public ConfigFile() throws IOException{
prymula <prymula76@outlook.com>
parents:
diff changeset
20 for (Map.Entry<?,?> e : System.getProperties().entrySet()) {
prymula <prymula76@outlook.com>
parents:
diff changeset
21 //System.out.println(String.format("%s = %s", e.getKey(), e.getValue()));
prymula <prymula76@outlook.com>
parents:
diff changeset
22 if (e.getKey().equals("user.dir")) {
prymula <prymula76@outlook.com>
parents:
diff changeset
23 userDir=String.format("%s", e.getValue());
prymula <prymula76@outlook.com>
parents:
diff changeset
24 }
prymula <prymula76@outlook.com>
parents:
diff changeset
25 if (e.getKey().equals("user.home")) {
prymula <prymula76@outlook.com>
parents:
diff changeset
26 userHome=String.format("%s", e.getValue());
prymula <prymula76@outlook.com>
parents:
diff changeset
27
prymula <prymula76@outlook.com>
parents:
diff changeset
28 }
prymula <prymula76@outlook.com>
parents:
diff changeset
29 if (e.getKey().equals("os.name")) {
prymula <prymula76@outlook.com>
parents:
diff changeset
30 osName=String.format("%s", e.getValue());
prymula <prymula76@outlook.com>
parents:
diff changeset
31
prymula <prymula76@outlook.com>
parents:
diff changeset
32 }
prymula <prymula76@outlook.com>
parents:
diff changeset
33 if (e.getKey().equals("os.version")) {
prymula <prymula76@outlook.com>
parents:
diff changeset
34 osVersion=String.format("%s", e.getValue());
prymula <prymula76@outlook.com>
parents:
diff changeset
35
prymula <prymula76@outlook.com>
parents:
diff changeset
36 }
prymula <prymula76@outlook.com>
parents:
diff changeset
37 if (e.getKey().equals("java.vm.version")) {
prymula <prymula76@outlook.com>
parents:
diff changeset
38 jvmVersion=String.format("%s", e.getValue());
prymula <prymula76@outlook.com>
parents:
diff changeset
39
prymula <prymula76@outlook.com>
parents:
diff changeset
40 }
prymula <prymula76@outlook.com>
parents:
diff changeset
41 }
prymula <prymula76@outlook.com>
parents:
diff changeset
42
prymula <prymula76@outlook.com>
parents:
diff changeset
43
prymula <prymula76@outlook.com>
parents:
diff changeset
44 File file=new File (userHome+name);
prymula <prymula76@outlook.com>
parents:
diff changeset
45 if (file.createNewFile()){
prymula <prymula76@outlook.com>
parents:
diff changeset
46 System.out.println("Plik zostal utworzony");
prymula <prymula76@outlook.com>
parents:
diff changeset
47 try {
prymula <prymula76@outlook.com>
parents:
diff changeset
48 FileWriter myWriter = new FileWriter(userHome+name);
prymula <prymula76@outlook.com>
parents:
diff changeset
49 myWriter.write("1");
prymula <prymula76@outlook.com>
parents:
diff changeset
50 myWriter.close();
prymula <prymula76@outlook.com>
parents:
diff changeset
51 System.out.println("Successfully wrote to the file.");
prymula <prymula76@outlook.com>
parents:
diff changeset
52 } catch (IOException e) {
prymula <prymula76@outlook.com>
parents:
diff changeset
53 System.out.println("An error occurred.");
prymula <prymula76@outlook.com>
parents:
diff changeset
54 e.printStackTrace();
prymula <prymula76@outlook.com>
parents:
diff changeset
55 }
prymula <prymula76@outlook.com>
parents:
diff changeset
56 }
prymula <prymula76@outlook.com>
parents:
diff changeset
57 else {
prymula <prymula76@outlook.com>
parents:
diff changeset
58 System.out.println("Plik istnieje");
prymula <prymula76@outlook.com>
parents:
diff changeset
59 }
prymula <prymula76@outlook.com>
parents:
diff changeset
60 }
prymula <prymula76@outlook.com>
parents:
diff changeset
61 public void save(boolean b){
prymula <prymula76@outlook.com>
parents:
diff changeset
62 String s="";
prymula <prymula76@outlook.com>
parents:
diff changeset
63 try {
prymula <prymula76@outlook.com>
parents:
diff changeset
64 FileWriter myWriter = new FileWriter(userHome+name);
prymula <prymula76@outlook.com>
parents:
diff changeset
65 if (b) s="1";
prymula <prymula76@outlook.com>
parents:
diff changeset
66 else s="0";
prymula <prymula76@outlook.com>
parents:
diff changeset
67 myWriter.write(s);
prymula <prymula76@outlook.com>
parents:
diff changeset
68 myWriter.close();
prymula <prymula76@outlook.com>
parents:
diff changeset
69 System.out.println("Successfully wrote to the file.");
prymula <prymula76@outlook.com>
parents:
diff changeset
70 } catch (IOException e) {
prymula <prymula76@outlook.com>
parents:
diff changeset
71 System.out.println("An error occurred.");
prymula <prymula76@outlook.com>
parents:
diff changeset
72 e.printStackTrace();
prymula <prymula76@outlook.com>
parents:
diff changeset
73 }
prymula <prymula76@outlook.com>
parents:
diff changeset
74 }
prymula <prymula76@outlook.com>
parents:
diff changeset
75 public boolean load() {
prymula <prymula76@outlook.com>
parents:
diff changeset
76 String data = "";
prymula <prymula76@outlook.com>
parents:
diff changeset
77 boolean r =false;
prymula <prymula76@outlook.com>
parents:
diff changeset
78 try {
prymula <prymula76@outlook.com>
parents:
diff changeset
79 File myObj = new File(userHome+name);
prymula <prymula76@outlook.com>
parents:
diff changeset
80 Scanner myReader = new Scanner(myObj);
prymula <prymula76@outlook.com>
parents:
diff changeset
81 while (myReader.hasNextLine()) {
prymula <prymula76@outlook.com>
parents:
diff changeset
82 data = myReader.nextLine();
prymula <prymula76@outlook.com>
parents:
diff changeset
83 System.out.println(data);
prymula <prymula76@outlook.com>
parents:
diff changeset
84 }
prymula <prymula76@outlook.com>
parents:
diff changeset
85 myReader.close();
prymula <prymula76@outlook.com>
parents:
diff changeset
86 } catch (FileNotFoundException e) {
prymula <prymula76@outlook.com>
parents:
diff changeset
87 System.out.println("An error occurred.");
prymula <prymula76@outlook.com>
parents:
diff changeset
88 e.printStackTrace();
prymula <prymula76@outlook.com>
parents:
diff changeset
89 }
prymula <prymula76@outlook.com>
parents:
diff changeset
90
prymula <prymula76@outlook.com>
parents:
diff changeset
91 if (data.equals("1")) r=true;
prymula <prymula76@outlook.com>
parents:
diff changeset
92
prymula <prymula76@outlook.com>
parents:
diff changeset
93 return r;
prymula <prymula76@outlook.com>
parents:
diff changeset
94 }
prymula <prymula76@outlook.com>
parents:
diff changeset
95
prymula <prymula76@outlook.com>
parents:
diff changeset
96 }