-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileManager.java
196 lines (171 loc) · 6.2 KB
/
FileManager.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
public class FileManager {
public FileManager() {
}
public void deleteManager(String target) throws IOException {
File oldFile = new File("data.dict");
File newFile = new File("another.dict");
if(!newFile.exists()) {
newFile.createNewFile();
}
Scanner sc = new Scanner(oldFile);
FileWriter fw = new FileWriter("another.dict");
fw.flush();
String temp;
boolean deleted = false;
// Exceptional case
if(target.equals("a")) {
temp = sc.nextLine();
while(!temp.equals("")) {
temp = sc.nextLine();
}
deleted = true;
}
while(sc.hasNextLine()) {
temp = sc.nextLine();
if(!deleted && temp.length() > target.length() && temp.substring(1, target.length() + 1).equals(target)) {
temp = sc.nextLine();
while(sc.hasNextLine() && Character.compare(temp.charAt(0), '@') != 0) {
temp = sc.nextLine();
if(temp.equals("")) {
break;
}
}
}
else {
fw.write(temp + "\n");
}
}
sc.close();
fw.close();
oldFile.delete();
Path source = Paths.get("another.dict");
Files.move(source, source.resolveSibling("data.dict"));
}
public void addManager(String lexicon, String definition, Dictionary dict) throws IOException {
Word word = new Word(lexicon, definition);
File oldFile = new File("data.dict");
File newFile = new File("another.dict");
if(!newFile.exists()) {
newFile.createNewFile();
}
Scanner sc = new Scanner(oldFile);
FileWriter fw = new FileWriter("another.dict");
fw.flush();
String temp = sc.nextLine();
int indexToAdd = dict.getIndexToAdd(word);
System.out.println(indexToAdd);
boolean added = false;
int i = 0;
// Case when add at the beginning
if(lexicon.compareTo(dict.vocabList.get(0)) < 0) {
fw.write("@" + lexicon + "\n" + definition + "\n" + "\n");
added = true;
}
fw.write(temp + "\n");
// Read and write file
while(sc.hasNextLine()) {
temp = sc.nextLine();
if(temp.equals("") && !added && i + 1 == indexToAdd) {
fw.write("\n" + "@" + lexicon + "\n" + definition + "\n" + "\n");
added = true;
}
else {
fw.write(temp + "\n");
if(!added && temp.length() > 0 && Character.compare(temp.charAt(0), '@') == 0) {
i++;
}
}
}
sc.close();
fw.close();
oldFile.delete();
Path source = Paths.get("another.dict");
Files.move(source, source.resolveSibling("data.dict"));
}
public void editDefinitionManager(String lexicon, String newDefinition) throws IOException {
File oldFile = new File("data.dict");
File newFile = new File("another.dict");
if(!newFile.exists()) {
newFile.createNewFile();
}
Scanner sc = new Scanner(oldFile);
FileWriter fw = new FileWriter("another.dict");
fw.flush();
String temp;
while(sc.hasNextLine()) {
temp = sc.nextLine();
if(temp.length() > lexicon.length() && temp.substring(1, lexicon.length() + 1).equals(lexicon)) {
fw.write(temp + "\n" + newDefinition + "\n" + "\n");
temp = sc.nextLine();
while(!temp.equals("") && sc.hasNextLine()) {
temp = sc.nextLine();
}
}
else {
fw.write(temp + "\n");
}
}
sc.close();
fw.close();
oldFile.delete();
Path source = Paths.get("another.dict");
Files.move(source, source.resolveSibling("data.dict"));
}
public void editLexiconManager(String oldLexicon, String newLexicon, String definition, Dictionary dict) throws IOException {
Word word = new Word(newLexicon, definition);
File oldFile = new File("data.dict");
File newFile = new File("another.dict");
if(!newFile.exists()) {
newFile.createNewFile();
}
Scanner sc = new Scanner(oldFile);
FileWriter fw = new FileWriter("another.dict");
fw.flush();
String temp = sc.nextLine();
boolean deleted = false;
boolean added = false;
int indexToAdd = dict.getIndexToAdd(word);
int i = 0;
// Case 0: when add at the beginning
if(newLexicon.compareTo(dict.vocabList.get(0)) < 0) {
fw.write("@" + newLexicon + "\n" + definition + "\n" + "\n");
added = true;
}
fw.write(temp + "\n");
// Read and write file
while(sc.hasNextLine()) {
temp = sc.nextLine();
if(temp.equals("") && !added && indexToAdd == i) {
fw.write("\n" + "@" + newLexicon + "\n" + definition + "\n" + "\n");
added = true;
}
else if(!deleted && temp.length() > oldLexicon.length() && temp.substring(1, oldLexicon.length() + 1).equals(oldLexicon)) {
temp = sc.nextLine();
while(sc.hasNextLine() && Character.compare(temp.charAt(0), '@') != 0) {
temp = sc.nextLine();
if(temp.equals("")) {
break;
}
}
}
else {
fw.write(temp + "\n");
if(!added && temp.length() > 0 && Character.compare(temp.charAt(0), '@') == 0) {
i++;
}
}
}
sc.close();
fw.close();
oldFile.delete();
Path source = Paths.get("another.dict");
Files.move(source, source.resolveSibling("data.dict"));
}
}