-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveAndLoad.java
More file actions
51 lines (42 loc) · 1.25 KB
/
SaveAndLoad.java
File metadata and controls
51 lines (42 loc) · 1.25 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
package almostNothing;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import robocode.*;
public class SaveAndLoad {
public static HashMap<String, Double> load(String filename) throws IOException {
HashMap<String, Double> qTable = new HashMap<String, Double>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(filename));
String line = reader.readLine();
while (line != null) {
String splitLine[] = line.split("/");
qTable.put(splitLine[0], Double.parseDouble(splitLine[1]));
line= reader.readLine();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
reader.close();
}
return qTable;
}
public static void save(String filename, HashMap<String, Double> qTable) {
PrintStream ps = null;
try {
ps = new PrintStream(new RobocodeFileOutputStream(filename));
for (Map.Entry<String, Double> entry : qTable.entrySet()) {
ps.println(entry.getKey()+"/"+entry.getValue());
}
} catch (IOException e) {
System.out.println("error");
} finally {
ps.flush();
ps.close();
}
}
}