You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: app/pages/learn/01_tutorial/04_mastering-the-api/02_modern_io/index.md
+8-10Lines changed: 8 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ This article focuses on tasks that application programmers are likely to encount
10
10
11
11
The Java API supports many other tasks, which are explained in detail in the [Java I/O API tutorial](https://dev.java/learn/java-io/).
12
12
13
-
Modern, at the time of this writing, means features that are out of preview in Java 21. In particular:
13
+
This article focuses on API improvements since Java 8. In particular:
14
14
15
15
* UTF-8 is the default for I/O since Java 18 ([JEP 400](https://openjdk.org/jeps/400))
16
16
* The `java.nio.file.Files` class, which first appeared in Java 7, added useful methods in Java 8, 11, and 12
@@ -31,6 +31,8 @@ Here, `path` is an instance of `java.nio.Path`, obtained like this:
31
31
var path = Path.of("/usr/share/dict/words");
32
32
```
33
33
34
+
Before Java 18, you were strongly encouraged to specify the character encoding with any file operations that read or write strings. Nowadays, by far the most common character encoding is UTF-8, but for backwards compatibility, Java used the "platform encoding", which can be a legacy encoding on Windows. To ensure portability, text I/O operations needed parameters `StandardCharsets.UTF_8`. This is no longer necessary.
35
+
34
36
If you want the file as a sequence of lines, call
35
37
36
38
```
@@ -200,9 +202,13 @@ Here are the other methods for traversing directory entries:
200
202
Ever since Java 1.1, the `ZipInputStream` and `ZipOutputStream` classes provide an API for processing zip files. But the API is a bit clunky. Java 8 introduced a much nicer *zip file system*:
The `try`-with-resources statement ensures that the `close` method is called after the zip file operations. That method updates the zip file to reflect any changes in the file system.
211
+
206
212
You can then use the methods of the `Files` class. Here we get a list of all files in the zip file:
You can remove files with `Files.delete`. To add or replace files, simply use `Files.writeString` or `Files.write`.
221
227
222
-
You must close the file system so that the changes are written to the zip file. Call
223
-
224
-
```
225
-
fs.close();
226
-
```
227
-
228
-
or use a `try`-with-resources statement.
229
-
230
228
### Creating Temporary Files and Directories
231
229
232
230
Fairly often, I need to collect user input, produce files, and run an external process. Then I use temporary files, which are gone after the next reboot, or a temporary directory that I erase after the process has completed.
0 commit comments