Skip to content

Commit 3658510

Browse files
committed
Incorporated more comments from danthe1st
1 parent fb1a838 commit 3658510

File tree

1 file changed

+8
-10
lines changed
  • app/pages/learn/01_tutorial/04_mastering-the-api/02_modern_io

1 file changed

+8
-10
lines changed

app/pages/learn/01_tutorial/04_mastering-the-api/02_modern_io/index.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This article focuses on tasks that application programmers are likely to encount
1010

1111
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/).
1212

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:
1414

1515
* UTF-8 is the default for I/O since Java 18 ([JEP 400](https://openjdk.org/jeps/400))
1616
* 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:
3131
var path = Path.of("/usr/share/dict/words");
3232
```
3333

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+
3436
If you want the file as a sequence of lines, call
3537

3638
```
@@ -200,9 +202,13 @@ Here are the other methods for traversing directory entries:
200202
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*:
201203

202204
```
203-
FileSystem fs = FileSystems.newFileSystem(pathToZipFile);
205+
try (FileSystem fs = FileSystems.newFileSystem(pathToZipFile)) {
206+
. . .
207+
}
204208
```
205209

210+
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+
206212
You can then use the methods of the `Files` class. Here we get a list of all files in the zip file:
207213

208214
```
@@ -219,14 +225,6 @@ String contents = Files.readString(fs.getPath("/LICENSE"));
219225

220226
You can remove files with `Files.delete`. To add or replace files, simply use `Files.writeString` or `Files.write`.
221227

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-
230228
### Creating Temporary Files and Directories
231229

232230
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

Comments
 (0)