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
Also use `Files.lines` if you can naturally process lines with stream operations (such as `map`, `filter`).
49
+
Also use `Files.lines` if you can naturally process lines with stream operations (such as `map`, `filter`).
50
50
51
51
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.
52
52
@@ -58,6 +58,10 @@ To split your input into something else than lines, use a `java.util.Scanner`. F
58
58
Stream<String> tokens = new Scanner(path).useDelimiter("\\PL+").tokens();
59
59
```
60
60
61
+
The `Scanner` class also has methods for reading numbers, but it is generally simpler to read the input as one string per line, or a single string, and then parse it.
62
+
63
+
Be careful when parsing numbers from text files, since their format may be locale-dependent. For example, the input `100.000` is 100.0 in the US locale but 100000.0 in the German locale. Use `java.text.NumberFormat` for locale-specific parsing. Alternatively, you may be able to use `Integer.parseInt`/`Double.parseDouble`.
64
+
61
65
## Writing Text Files
62
66
63
67
You can write a string to a text file with a single call:
@@ -74,26 +78,23 @@ List<String> lines = . . .;
74
78
Files.write(path, lines);
75
79
```
76
80
77
-
For more general output, use a `PrintWriter`so that you can use the `printf` method:
81
+
For more general output, use a `PrintWriter`if you want to use the `printf` method:
78
82
79
83
```
80
84
var writer = new PrintWriter(path.toFile());
81
-
writer.printf("Hello, %s, next year you'll be %d years old!%n", name, age + 1);
85
+
writer.printf(locale, "Hello, %s, next year you'll be %d years old!%n", name, age + 1);
82
86
```
83
87
88
+
Note that `printf` is locale-specific. When writing numbers, be sure to write them in the appropriate format. Instead of using `printf`, consider `java.text.NumberFormat` or `Integer.toString`/`Double.toString`.
89
+
84
90
Weirdly enough, as of Java 21, there is no `PrintWriter` constructor with a `Path` parameter.
85
91
86
-
The `BufferedWriter` class can only write strings without formatting them. That is ok if you use the `String.formatted` method:
92
+
If you don't use `printf`, you can use the `BufferedWriter` class and write strings with the `write` method.
87
93
88
94
```
89
95
var writer = Files.newBufferedWriter(path);
90
-
writer.write("Hello, %s, next year you'll be %d years old!%n".formatted(name, age + 1));
91
-
```
92
-
93
-
Or, with the `FMT` template (which is still in preview):
94
-
95
-
```
96
-
writer.write(FMT."Hello, %s\{name}, next year you'll be %d\{age + 1} years old!%n");
96
+
writer.write(line); // Does not write a line separator
97
+
writer.newLine();
97
98
```
98
99
99
100
Remember to close the `writer` when you are done.
@@ -134,7 +135,7 @@ OutputStream out = Files.newOutputStream(path);
134
135
in.transferTo(out);
135
136
```
136
137
137
-
Nowadays, there is no need to read or write bytes, or chunks of bytes, in a loop.
138
+
Note that no loop is required if you simply want to read all bytes of an input stream.
138
139
139
140
But do you really need an input stream? Many APIs give you the option to read from a file or URL.
Here are the other methods for traversing directory entries:
190
191
191
192
* An overloaded version of `Files.walk` lets you limit the depth of the traversed tree.
192
-
* Two `Files.walkFileTree` methods provide more control over the iteration process, by notifying a `FileVisitor` when a directory is visited for the first and last time. This can be occasionally useful, in particularly for emptying and deleting a tree of directories. See the tutorial [=https://dev.java/learn/java-io/file-system/walking-tree/ Walking the File Tree](null) for details. Unless you need this control, use the simpler `Files.walk` method.
193
+
* Two `Files.walkFileTree` methods provide more control over the iteration process, by notifying a `FileVisitor` when a directory is visited for the first and last time. This can be occasionally useful, in particularly for emptying and deleting a tree of directories. See the tutorial [Walking the File Tree](https://dev.java/learn/java-io/file-system/walking-tree) for details. Unless you need this control, use the simpler `Files.walk` method.
193
194
* The `Files.find` method is just like `Files.walk`, but you provide a filter that inspects each path and its `BasicFileAttributes`. This is slightly more efficient than reading the attributes separately for each file.
194
195
* Two `Files.newDirectoryStream` methods yields `DirectoryStream` instances, which can be used in enhanced `for` loops. There is no advantage over using `Files.list`.
195
196
* The legacy `File.list` or `File.listFiles` methods return file names or `File` objects. These are now obsolete.
0 commit comments