forked from yfain/Java4Kids_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyTextFileWriter.java
More file actions
35 lines (24 loc) · 977 Bytes
/
Copy pathMyTextFileWriter.java
File metadata and controls
35 lines (24 loc) · 977 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
33
34
35
package niosamples;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.time.LocalDateTime;
public class MyTextFileWriter {
public static void main(String[] args){
System.out.println("The current directory is "+ System.getProperty("user.dir"));
String myScore = "My game score is 28000 " + LocalDateTime.now() + "\n";
Path path = Paths.get("data/scores.txt");
try {
if ( Files.exists(path)){
Files.write(path, myScore.getBytes(), StandardOpenOption.APPEND);
} else {
Files.write(path, myScore.getBytes(), StandardOpenOption.CREATE);
}
System.out.println("The game score was saved at " + path.toAbsolutePath());
} catch (IOException ioe) {
System.out.println("Can't write file: " + ioe.getMessage());
}
}
}