-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileRW.java
More file actions
207 lines (193 loc) · 4.99 KB
/
FileRW.java
File metadata and controls
207 lines (193 loc) · 4.99 KB
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
197
198
199
200
201
202
203
204
205
206
207
/*********************************************************
* Filename: FileRW.java
* Author: Charles Walker
* Created: 11/08/23
* Modified: 12/1/23
* Added the fileRecord attribute and writeToFile method so the class can record the final results of the population.
*
* Purpose:
* The class is responsible for all of the reading from and writing to
* files throughout the programs process.
*
* Attributes:
* -fileData: File
* -data: String[][]
* -previousPop: File
* -lastSession: File
* -testCase: File
*
* Methods:
* +readFromFile(): void
* +startFromFile(): void
* +startFromFileTest(): void
* +rowCountTest(): void
* +writeToFileTest(String[][], String, int): void
* +rowCount(): void
* +writeToFile(String[][], String, int): void
* +parseDistances(): double[][]
* +createLocations(): String[]
*
*********************************************************/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
public class FileRW {
File fileData = new File("distances.csv");
String[][] data = new String[50][50];
int[][] previousPop;
File lastSession = new File("Last Session.csv");
File testCase = new File("TestStart.csv");
public void readFromFile() {
try {
data = new String[50][50];
FileReader fr = new FileReader("distances.csv");
BufferedReader br = new BufferedReader(fr);
String row;
for(int i = 0; i < 50; i++) {
row = br.readLine();
int numOfColumns = 50;
String[] columns = row.split(",", numOfColumns);
for(int j = 0; j < 50; j++) {
data[i][j] = columns[j];
data[i][j] = data[i][j].replace(",","");
}
}
br.close();
fr.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public int[][] startFromFile() {
try {
int rows = 0;
int rowCount=rowCount()-1;
previousPop = new int[rowCount][49];
FileReader fr = new FileReader("Last Session.csv");
BufferedReader br = new BufferedReader(fr);
String row;
br.readLine();
while ((row = br.readLine())!=null) {
String[] columns = row.split(",", 50);
for (int i = 0; i < columns.length; i++) {
columns[i] = columns[i].replace(" ", "");
}
for(int j = 0; j < columns.length-1; j++) {
previousPop[rows][j] = Integer.parseInt(columns[j]);
}
rows++;
}
br.close();
fr.close();
}catch (Exception e) {
e.printStackTrace();
}
return previousPop;
}
public int[][] startFromFileTest() {
try {
int rows = 0;
int rowCount=rowCountTest();
previousPop = new int[rowCount][49];
FileReader frt = new FileReader("TestStart.csv");
BufferedReader brt = new BufferedReader(frt);
String row;
while ((row = brt.readLine())!=null) {
String[] columns = row.split(",", 50);
for (int i = 0; i < columns.length; i++) {
columns[i] = columns[i].trim();
columns[i] = columns[i].replace(" ", "");
}
for(int j = 0; j < columns.length-1; j++) {
previousPop[rows][j] = Integer.parseInt(columns[j]);
}
rows++;
}
brt.close();
frt.close();
}catch (Exception e) {
e.printStackTrace();
}
return previousPop;
}
public int rowCountTest() {
int count = 0;
try {
FileReader fr = new FileReader("TestStart.csv");
BufferedReader br = new BufferedReader(fr);
count = 0;
while (br.readLine() != null) {
count++;
}
br.close();
fr.close();
}catch (Exception e) {
e.printStackTrace();
}
return count;
}
public void writeToFileTest(String[][] data1, String data2, int popSize) {
try {
FileWriter fw = new FileWriter("TestEnd.csv");
fw.write(data2+"\n");
for (int i = 0; i < popSize; i++) {
for(int j = 0; j < 50; j++) {
fw.write(data1[i][j]+", ");
}
fw.write("\n");
}
fw. close();
}catch (Exception e) {
e.printStackTrace();
}
}
public int rowCount() {
int count = 0;
try {
FileReader fr = new FileReader("Last Session.csv");
BufferedReader br = new BufferedReader(fr);
count = 0;
while (br.readLine() != null) {
count++;
}
br.close();
fr.close();
}catch (Exception e) {
e.printStackTrace();
}
return count;
}
public void writeToFile(String[][] data1, String data2, int popSize) {
try {
FileWriter fw = new FileWriter("Last Session.csv");
fw.write(data2+"\n");
for (int i = 0; i < popSize; i++) {
for(int j = 0; j < 50; j++) {
fw.write(data1[i][j]+", ");
}
fw.write("\n");
}
fw. close();
}catch (Exception e) {
e.printStackTrace();
}
}
public double[][] parseDistances() {
double[][] distances = new double[49][49];
for(int i = 1; i < 50; i++) {
for(int j = 1; j < 50; j++) {
distances[i-1][j-1] = Double.parseDouble(data[i][j]);
}
}
return distances;
}
public String[] createLocations() {
String[] locations = new String[49];
for(int i = 1; i < 50; i++) {
locations[i-1] = data[i][0];
}
return locations;
}
}