1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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;
}
}
|