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

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