forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectoryStreamRecursiveEx.java
More file actions
38 lines (27 loc) · 923 Bytes
/
DirectoryStreamRecursiveEx.java
File metadata and controls
38 lines (27 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.zetcode;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class DirectoryStreamRecursiveEx {
private static List<Path> paths = new ArrayList<>();
private static List<Path> walk(Path path) throws IOException {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
for (Path entry : stream) {
if (Files.isDirectory(entry)) {
walk(entry);
}
paths.add(entry);
}
}
return paths;
}
public static void main(String[] args) throws IOException {
var myPath = Paths.get("C:/Users/Jano/Downloads");
var paths = walk(myPath);
paths.forEach(path -> System.out.println(path));
}
}