-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReaderAndWriter.java
More file actions
256 lines (151 loc) · 4.88 KB
/
Copy pathReaderAndWriter.java
File metadata and controls
256 lines (151 loc) · 4.88 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
public class ReaderAndWriter {
public static String read(String filePath) {
File file = new File(filePath);
Scanner sc;
String s = "";
try {
sc = new Scanner(file);
while (sc.hasNextLine()) {
s+=sc.nextLine();
}
sc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(s.equals(""))
s= "0";
return s;
}
public static void write(String filePath, String content) {
File file = new File(filePath);
try {
file.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedWriter writer;
try {
writer = new BufferedWriter(new FileWriter(file));
writer.write(content);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void createDirectory(String folderName) {
File dir = new File(folderName);
if(!dir.exists()) {
dir.mkdirs();
}
}
public static boolean learnedExists(String path) {
File dir = new File(path + "learned");
return dir.exists();
}
public static int filesInDir(String path) {
File dir = new File(path);
return dir.listFiles().length;
}
public static boolean fileExists(String filePath) {
File file = new File(filePath);
return file.exists();
}
/**
* in this case gets the folders in learing and returns avg
* @param filePathRead the base file path
* @param amountRan the amount read
* @return avg values
*/
public static ArrayList<Double> readLearned(String filePathRead, int amountRan) {
File folder = new File(filePathRead + "learning/"+0+"/roads");
ArrayList<Double> sum = new ArrayList<Double>();
ArrayList<Double> avg = new ArrayList<Double>();
HashMap<Integer, ArrayList<Double>> roads = readLearnedRoads(filePathRead, amountRan);
for(int i =0; i < roads.size();i++) {
sum.add(0.0);
}
//gets the sum of all the values for each road
for(int i = 0; i < roads.size(); i++) {
for(int j = 0; j < amountRan; j++) {
sum.set(i, sum.get(i) + roads.get(i).get(j));
}
}
//gets the avg
for(int i = 0; i < sum.size(); i++) {
avg.add(((double)sum.get(i))/amountRan);
}
return avg;
}
/**
* in this case gets the folders in learing and returns the standar deviation
* @param filePathRead the base file path
* @param amountRan the amount read
* @return avg values
*/
public static ArrayList<Double> stdOfEachRoad(String filePathRead, int amountRan) {
ArrayList<Double> std = new ArrayList<Double>();
ArrayList<Double> sum = new ArrayList<Double>();
ArrayList<Double> avg = readLearned(filePathRead, amountRan);
HashMap<Integer, ArrayList<Double>> roads = readLearnedRoads(filePathRead, amountRan);
for(int i =0; i < roads.size();i++) {
sum.add(0.0);
}
for(int i =0; i < avg.size(); i++) {
for(int j = 0; j < roads.get(i).size(); j++) {
sum.set(i, sum.get(i) + Math.pow(avg.get(i)-roads.get(i).get(j), 2));
}
}
for(int i =0; i < avg.size(); i++) {
double bessel = roads.get(i).size()-1;
double before = sum.get(i) / bessel;
std.add(Math.sqrt(before));
}
return std;
}
public static HashMap<Integer, ArrayList<Double>> readLearnedRoads(String filePathRead, int amountRan) {
File folder = new File(filePathRead + "learning/"+0+"/roads/");
int amtRoad = folder.listFiles().length;
HashMap<Integer, ArrayList<Double>> roads = new HashMap<Integer, ArrayList<Double>>();
//reads the files for the road
for(int j = 0; j < amtRoad; j++) {
ArrayList<Double> a;
if(roads.get(j) == null) {
a = new ArrayList<Double>();
roads.put(j, a);
}
else {
a = roads.get(j);
}
for(int i = 0; i < amountRan; i++){
double readVal = Double.parseDouble(read(filePathRead + "learning/"+i+"/roads/" + j +".txt"));
a.add(readVal);
roads.put(j, a);
}
}
return roads;
}
public static void readSoundFile(String fileName) {
File f = new File(fileName);
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(f);
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.out.println("error");
System.out.println(new File(fileName).exists());
}
}
}