Skip to content

Commit 5bde670

Browse files
authored
minor changes to modern IO article (java#121)
1 parent 457e0df commit 5bde670

File tree

1 file changed

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

1 file changed

+5
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ toc:
1414
- The Files API {the-files-api}
1515
- Conclusion {conclusion}
1616
last_update: 2024-04-24
17+
last_review: 2024-08-05
1718
description: "This article focuses on tasks that application programmers are likely to encounter, particularly in web applications, such as reading and writing text files, reading text, images, JSON from the web, and more."
1819
author: ["CayHorstmann"]
1920
---
@@ -155,8 +156,9 @@ String result = new String(bytes);
155156
Or transfer the data to an output stream:
156157

157158
```java
158-
OutputStream out = Files.newOutputStream(path);
159-
in.transferTo(out);
159+
try(OutputStream out = Files.newOutputStream(path)) {
160+
in.transferTo(out);
161+
}
160162
```
161163

162164
Note that no loop is required if you simply want to read all bytes of an input stream.
@@ -216,7 +218,7 @@ Here are the other methods for traversing directory entries:
216218
* An overloaded version of [`Files.walk`](javadoc:Files.walk(Path,depth)) lets you limit the depth of the traversed tree.
217219
* Two [`Files.walkFileTree`](javadoc:Files.walkFileTree(Path)) methods provide more control over the iteration process, by notifying a [`FileVisitor`](javadoc: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](id:api.javaio.file_sytem.walking_tree) for details. Unless you need this control, use the simpler [`Files.walk`](javadoc:Files.walk(Path)) method.
218220
* The [`Files.find`](javadoc:Files.find(Path)) method is just like [`Files.walk`](javadoc:Files.walk(Path)), but you provide a filter that inspects each path and its [`BasicFileAttributes`](javadoc:BasicFileAttributes). This is slightly more efficient than reading the attributes separately for each file.
219-
* Two [`Files.newDirectoryStream(Path)`](javadoc:Files.newDirectoryStream(Path)) methods yields [`DirectoryStream`](javadoc:DirectoryStream) instances, which can be used in enhanced `for` loops. There is no advantage over using [`Files.list`](javadoc:Files.list(Path)).
221+
* Two [`Files.newDirectoryStream(Path)`](javadoc:Files.newDirectoryStream(Path)) methods yield [`DirectoryStream`](javadoc:DirectoryStream) instances, which can be used in enhanced `for` loops. There is no advantage over using [`Files.list`](javadoc:Files.list(Path)).
220222
* The legacy [`File.list`](javadoc:File.list()) or [`File.listFiles`](javadoc:File.listFiles()) methods return file names or [`File`](javadoc:File) objects. These are now obsolete.
221223

222224
### Working with ZIP Files

0 commit comments

Comments
 (0)