-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScoreWriter.java
More file actions
32 lines (25 loc) · 817 Bytes
/
ScoreWriter.java
File metadata and controls
32 lines (25 loc) · 817 Bytes
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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class ScoreWriter {
public static void writeNewHighscore(int score) throws IOException {
FileWriter myWriter = new FileWriter("Highscore.txt");
myWriter.write(String.valueOf(score));
myWriter.close();
}
public static int readScores() throws FileNotFoundException {
String data = "";
File highscore = new File("Highscore.txt");
Scanner myReader = new Scanner(highscore);
while (myReader.hasNextLine()) {
data = myReader.nextLine();
}
myReader.close();
if (data.trim().isEmpty()) {
return 0;
}
return Integer.parseInt(data);
}
}