-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileIO.java
129 lines (116 loc) · 2.91 KB
/
FileIO.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
package etf.dotsandboxes.ma170420d;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JTextArea;
import etf.dotsandboxes.ma170420d.Board.Edge;
public class FileIO {
private File readFile, writeFile;
private int m, n;
private JTextArea ta;
private ArrayList<String> moves = new ArrayList<>();
public Map<String, Edge> hashMap = new HashMap<>();
public Map<Edge, String> toStringMap = new HashMap<>();
public FileIO (File readFile, File writeFile, int m, int n) {
this.readFile = readFile;
this.writeFile = writeFile;
this.m = m;
this.n = n;
if (writeFile != null) {
try {
FileWriter fw = new FileWriter(writeFile, false);
fw.flush();
fw.close();
} catch (IOException e) {
}
}
}
public File getReadFile() {
return readFile;
}
public boolean read() {
try {
BufferedReader br = new BufferedReader(new FileReader(readFile));
String line;
if ((line = br.readLine()) != null) {
if (line.length() != 3 || line.charAt(1) != ' ') {
br.close();
throw new IOException();
}
m = Integer.parseInt(line.substring(0, 1));
n = Integer.parseInt(line.substring(2, 3));
}
while ((line = br.readLine()) != null) {
if (line.length() != 2) {
br.close();
throw new IOException();
}
moves.add(line);
}
br.close();
return true;
}
catch (FileNotFoundException e) {
System.err.println("File not found...");
return false;
}
catch (IOException e) {
System.err.println("Bad file format...");
return false;
}
catch(NumberFormatException e) {
System.err.println("Bad file format...");
return false;
}
}
public void write(String text) {
ta.append(text+"\n");
if (writeFile == null) return;
try {
FileWriter fw = new FileWriter(writeFile, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw);
out.println(text);
out.close();
} catch (IOException e) {
System.err.println("Writing in file error...");
}
}
public void setTextArea(JTextArea ta) {
this.ta = ta;
}
public int getM() {
return m;
}
public int getN() {
return n;
}
public ArrayList<String> getMoves() {
return moves;
}
public void makeHashMap(Edge[][] horizontal, Edge[][] vertical) {
String[] letters = {"A", "B", "C", "D", "E", "F", "G", "H", "I"};
for (int i = 0; i < m + 1; i++) {
for (int j = 0; j < n + 1; j++) {
if (j < n) {
String key = "" + i + letters[j];
hashMap.put(key, horizontal[i][j]);
toStringMap.put(horizontal[i][j], key);
}
if (i < m) {
String key = letters[i] + j;
hashMap.put(key, vertical[i][j]);
toStringMap.put(vertical[i][j], key);
}
}
}
}
}