Skip to content

Commit fb1a838

Browse files
committed
Incorporated comments from danthe1st
1 parent 2503117 commit fb1a838

File tree

1 file changed

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

1 file changed

+14
-13
lines changed

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ try (Stream<String> lines = Files.lines(path))
4646
}
4747
```
4848

49-
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`).
5050

5151
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.
5252

@@ -58,6 +58,10 @@ To split your input into something else than lines, use a `java.util.Scanner`. F
5858
Stream<String> tokens = new Scanner(path).useDelimiter("\\PL+").tokens();
5959
```
6060

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+
6165
## Writing Text Files
6266

6367
You can write a string to a text file with a single call:
@@ -74,26 +78,23 @@ List<String> lines = . . .;
7478
Files.write(path, lines);
7579
```
7680

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

7983
```
8084
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);
8286
```
8387

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+
8490
Weirdly enough, as of Java 21, there is no `PrintWriter` constructor with a `Path` parameter.
8591

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.
8793

8894
```
8995
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();
9798
```
9899

99100
Remember to close the `writer` when you are done.
@@ -134,7 +135,7 @@ OutputStream out = Files.newOutputStream(path);
134135
in.transferTo(out);
135136
```
136137

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.
138139

139140
But do you really need an input stream? Many APIs give you the option to read from a file or URL.
140141

@@ -189,7 +190,7 @@ try (Stream<Path> entries = Files.walk(pathToDirectory)) {
189190
Here are the other methods for traversing directory entries:
190191

191192
* 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.
193194
* 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.
194195
* Two `Files.newDirectoryStream` methods yields `DirectoryStream` instances, which can be used in enhanced `for` loops. There is no advantage over using `Files.list`.
195196
* The legacy `File.list` or `File.listFiles` methods return file names or `File` objects. These are now obsolete.

0 commit comments

Comments
 (0)