-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWrite.java
99 lines (82 loc) · 2.89 KB
/
Write.java
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
97
98
99
package database;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.FileReader;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* loads and writes database.
* */
public class Write {
/**
* load database from a text file.
**/
public static void writes() {
Scanner input = new Scanner(System.in);
System.out.println("Give me the file name of the database");
try {
String fileinput = input.next();
if (fileinput.equals("cancel")) {
return;
}
File f = new File(fileinput + ".txt");
BufferedReader sc = new BufferedReader(new FileReader(fileinput + ".txt"));
if (!(f.exists())) {
throw new Exception();
}
String line;
while ((line = sc.readLine()) != null) {
List<String> valuedata = new ArrayList<String>();
String again;
if (line.toLowerCase().contains("fields")) {
again = line.split("=")[1].trim();
for (String retval : again.split(",")) {
Fields.getFields().add(retval);
Fields.setFieldscounter(Fields.getFieldscounter() + 1);
}
} else {
again = line.split("=")[1].trim();
for (String retval : again.split(",")) {
valuedata.add(retval);
}
CreateData.getValues().put(CreateData.getCounter(), valuedata);
CreateData.setCounter(CreateData.getCounter() + 1);
}
}
System.out.println("Loaded!");
} catch (Exception e) {
System.out.println("File cannot be found.If you want to stop, type cancel");
writes();
}
}
/**
* writes/copy database into a text file.
**/
public static void writedata() {
Scanner input = new Scanner(System.in);
try {
System.out.println("Give me the name of the file you want to save ");
String files = input.next();
PrintWriter pr = new PrintWriter(new File((files + ".txt")));
pr.print("fields=");
for (int i = 0; i < Fields.getFields().size(); i++) {
pr.print(Fields.getFields().get(i) + ",");
}
pr.println();
CreateData.getValues().forEach((key, value) -> {
pr.print(key + "=");
for (int i = 0; i < value.size(); i++) {
pr.print(value.get(i) + ",");
}
pr.println();
});
System.out.println("File saved!");
pr.close();
} catch (Exception e) {
System.err.println(e);
}
Database.menu();
}
}