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
+40-44Lines changed: 40 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ This article focuses on tasks that application programmers are likely to encount
5
5
* Reading and writing text files
6
6
* Reading text, images, JSON from the web
7
7
* Visiting files in a directory
8
-
* Reading a zip file
8
+
* Reading a ZIP file
9
9
* Creating a temporary file or directory
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/).
@@ -21,42 +21,39 @@ This article focuses on API improvements since Java 8. In particular:
21
21
22
22
Tou can read a text file into a string like this:
23
23
24
-
```
24
+
```java
25
25
String content =Files.readString(path);
26
26
```
27
27
28
28
Here, `path` is an instance of `java.nio.Path`, obtained like this:
29
29
30
-
```
30
+
```java
31
31
var path =Path.of("/usr/share/dict/words");
32
32
```
33
33
34
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
35
36
36
If you want the file as a sequence of lines, call
37
37
38
-
```
38
+
```java
39
39
List<String> lines =Files.readAllLines(path);
40
40
```
41
41
42
42
If the file is large, process the lines lazily as a `Stream<String>`:
43
43
44
-
```
45
-
try (Stream<String> lines = Files.lines(path))
46
-
{
47
-
. . .
44
+
```java
45
+
try (Stream<String> lines =Files.lines(path)) {
46
+
...
48
47
}
49
48
```
50
49
51
-
Also use `Files.lines` if you can naturally process lines with stream operations (such as `map`, `filter`).
52
-
53
-
Note that the stream returned by `Files.lines` needs to be closed. To ensure that this happens, use a `try`-with-resources statement, as in the preceding code snippet.
50
+
Also use `Files.lines` if you can naturally process lines with stream operations (such as `map`, `filter`). Note that the stream returned by `Files.lines` needs to be closed. To ensure that this happens, use a `try`-with-resources statement, as in the preceding code snippet.
54
51
55
52
There is no longer a good reason to use the `readLine` method of `java.io.BufferedReader`.
56
53
57
54
To split your input into something else than lines, use a `java.util.Scanner`. For example, here is how you can read words, separated by non-letters:
That is overkill if all you want is the data. Instead, use:
121
118
122
-
```
119
+
```java
123
120
InputStream in =newURI("https://horstmann.com/index.html").toURL().openStream();
124
121
```
125
122
126
123
Then read the data into a byte array and optionally turn them into a string:
127
124
128
-
```
125
+
```java
129
126
byte[] bytes = in.readAllBytes();
130
127
String result =newString(bytes);
131
128
```
132
129
133
130
Or transfer the data to an output stream:
134
131
135
-
```
132
+
```java
136
133
OutputStream out =Files.newOutputStream(path);
137
134
in.transferTo(out);
138
135
```
@@ -143,14 +140,14 @@ But do you really need an input stream? Many APIs give you the option to read fr
143
140
144
141
Your favorite JSON library is likely to have methods for reading from a file or URL. For example, with [Jackson jr](https://github.com/FasterXML/jackson-jr):
@@ -197,29 +193,29 @@ Here are the other methods for traversing directory entries:
197
193
* Two `Files.newDirectoryStream` methods yields `DirectoryStream` instances, which can be used in enhanced `for` loops. There is no advantage over using `Files.list`.
198
194
* The legacy `File.list` or `File.listFiles` methods return file names or `File` objects. These are now obsolete.
199
195
200
-
### Working with Zip Files
196
+
### Working with ZIP Files
201
197
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*:
198
+
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.
206
+
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
207
212
-
You can then use the methods of the `Files` class. Here we get a list of all files in the zip file:
208
+
You can then use the methods of the `Files` class. Here we get a list of all files in the ZIP file:
0 commit comments