-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScoreHistoryFile.java
44 lines (36 loc) · 1.26 KB
/
ScoreHistoryFile.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
/**
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
import java.util.*;
import java.io.*;
public class ScoreHistoryFile implements ScoreHistoryReader, ScoreHistoryWriter {
private static String SCOREHISTORY_DAT = "SCOREHISTORY.DAT";
public void addScore(String nick, String date, String score)
throws IOException, FileNotFoundException {
String data = nick + "\t" + date + "\t" + score + "\n";
RandomAccessFile out = new RandomAccessFile(SCOREHISTORY_DAT, "rw");
out.skipBytes((int) out.length());
out.writeBytes(data);
out.close();
}
public Vector<Score> getScores(String nick)
throws IOException, FileNotFoundException {
Vector<Score> scores = new Vector<>();
BufferedReader in =
new BufferedReader(new FileReader(SCOREHISTORY_DAT));
String data;
while ((data = in.readLine()) != null) {
// File format is nick\tfname\te-mail
String[] scoredata = data.split("\t");
//"Nick: scoredata[0] Date: scoredata[1] Score: scoredata[2]
if (nick.equals(scoredata[0])) {
scores.add(new Score(scoredata[0], scoredata[1], scoredata[2]));
}
}
return scores;
}
}