-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileManager.java
103 lines (90 loc) · 3.22 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
import java.io.IOException;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Path;
class FileManager {
ArrayList<String> readAllWordFromFile(String fileName){
ArrayList<String> data = new ArrayList<String>();
String line;
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
while ((line = reader.readLine()) != null) {
data.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
void writeCorrectWord(String fileName, String userInput){
try (BufferedWriter writter = new BufferedWriter(new FileWriter(fileName, true))) {
writter.write(userInput+"\n");
} catch (IOException e) {
e.printStackTrace();
}
}
void writeWordWithIndexForResume(ArrayList<Word> boardWordList, String fileName){
try (BufferedWriter writter = new BufferedWriter(new FileWriter(fileName))) {
for(int i=0;i<boardWordList.size();i++){
writter.write(boardWordList.get(i).word+"|"+boardWordList.get(i).X+"|"+boardWordList.get(i).Y+"\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
void displayCorrectWord(String fileName){
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
int count=1;
String line;
while ((line = br.readLine()) != null) {
System.out.println(count+". "+line);
count++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
void clearCorrectWordFile(String fileName){
try (BufferedWriter writter = new BufferedWriter(new FileWriter(fileName))) {
writter.write("");
} catch (IOException e) {
e.printStackTrace();
}
}
void readWordWithIndexForResume(ArrayList<Word> boardWordList, String fileName){
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = reader.readLine()) != null) {
String[] components = line.split("\\|");
Word word = new Word(components[0], Integer.parseInt(components[1]), Integer.parseInt(components[2]));
boardWordList.add(word);
}
} catch (IOException e) {
e.printStackTrace();
}
}
int correctWordLength(String fileName){
int lineCount = 0;
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = br.readLine()) != null) {
lineCount++;
}
} catch (IOException e) {
e.printStackTrace();
}
return lineCount;
}
boolean isFileEmpty(String fileName){
try{
Path path = Path.of(fileName);
return (Files.size(path)==0)?true:false;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}