-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModernFileIO.java
More file actions
119 lines (106 loc) · 3.92 KB
/
Copy pathModernFileIO.java
File metadata and controls
119 lines (106 loc) · 3.92 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package Phase8_PracticalAPIs.FileIO;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.stream.Stream;
/**
* Modern File I/O — Java 11 → 21 conveniences
* -------------------------------------------
* The text-handling shortcuts most of your code should reach for first.
* <p>
*
* Java 11
* -------
* Files.readString(path[, charset])
* Files.writeString(path, charSequence[, charset][, options...])
* String.lines() / String.repeat() / String.isBlank() / String.strip()
* <p>
*
* Java 12+
* --------
* String.indent(n) / String.transform(Function<String,R>)
* <p>
*
* Java 13+
* --------
* String.formatted(args)
* Text blocks ("""...""")
* <p>
*
* Java 16+
* --------
* Stream.toList() — convenient over Collectors.toList()
* <p>
*
* Java 17+
* --------
* Sealed type hierarchies + pattern matching make the surface around
* reading "different shapes of records" cleaner.
* <p>
*
* Java 21 hot tip
* ---------------
* Combine Files.lines + Stream.gather(...) (preview) for windowed
* text processing. For now we'll do it with the stable API.
*/
public class ModernFileIO {
public static void main(String[] args) throws IOException {
section("1) Files.writeString / readString — Java 11");
Path tmp = Files.createTempFile("modern-", ".md");
Files.writeString(tmp, """
# Modern Java I/O
Lines like this.
Another line.
""", StandardCharsets.UTF_8);
String all = Files.readString(tmp, StandardCharsets.UTF_8);
System.out.println("read:\n" + all);
section("2) Files.write(List<String>) — line-oriented");
Files.write(tmp, List.of("alpha", "beta", "gamma"),
StandardCharsets.UTF_8,
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
for (String s : Files.readAllLines(tmp, StandardCharsets.UTF_8)) {
System.out.println(" " + s);
}
section("3) Files.lines + Stream + toList()");
try (Stream<String> s = Files.lines(tmp, StandardCharsets.UTF_8)) {
List<String> upper = s.map(String::toUpperCase).toList(); // Java 16+
System.out.println(upper);
}
section("4) String.lines() — process an already-loaded string");
String content = """
one
two
three
""";
long n = content.lines().count();
System.out.println("string had " + n + " lines");
section("5) Append mode and createNew");
Path append = Files.createTempFile("modern-append-", ".log");
Files.writeString(append, "line 1\n", StandardCharsets.UTF_8);
Files.writeString(append, "line 2\n", StandardCharsets.UTF_8,
StandardOpenOption.APPEND);
System.out.println(Files.readString(append));
section("6) Files.mismatch — byte-compare two files (Java 12+)");
Path a = Files.createTempFile("a-", ".bin");
Path b = Files.createTempFile("b-", ".bin");
Files.writeString(a, "hello world");
Files.writeString(b, "hello WORLD");
long mismatch = Files.mismatch(a, b); // -1 if equal, else first differing byte
System.out.println("first differing byte = " + mismatch);
section("7) Files.copy with InputStream / OutputStream — handy bridge");
// e.g., piping an HTTP response straight to disk:
// Files.copy(httpResponse.body(), destPath);
Files.deleteIfExists(tmp);
Files.deleteIfExists(append);
Files.deleteIfExists(a);
Files.deleteIfExists(b);
section("done");
}
private static void section(String title) {
System.out.println();
System.out.println("=== " + title + " ===");
}
}