-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jdk11.java
32 lines (28 loc) · 978 Bytes
/
Jdk11.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
package samples.jdk11;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class Jdk11 {
public static void main(String[] args) {
fileHandling();
}
/**
* Shows how to use a single method to write content to a file
* and another single one to read.
* That makes it really easy to handle files in Java.
* It's even easer than {@link Files#lines(Path)}
* introduced in Java 8 (which has the porpuse to process
* each content file at a time).
*/
private static void fileHandling() {
try {
Path fileName = Files.createTempFile("", "my-file.txt");
Path filePath = Files.writeString(fileName, "File content");
String fileContent = Files.readString(filePath);
System.out.println(fileContent);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}