Skip to content

Commit 2588ce0

Browse files
authored
Create ListFile (nio)
1 parent 7b84ffb commit 2588ce0

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

File/ListFile (nio)

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.io.IOException;
2+
import java.nio.file.DirectoryStream;
3+
import java.nio.file.*;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
public class dd {
11+
12+
private static List<Path> paths = new ArrayList<>();
13+
14+
private static List<Path> walk(Path path) throws IOException {
15+
16+
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path))
17+
{
18+
Path dummy = Paths.get(" ");
19+
20+
for (Path entry : stream) {
21+
if (Files.isDirectory(entry)) {
22+
paths.add(entry.getFileName());
23+
paths.add(dummy);
24+
walk(entry);
25+
26+
}
27+
else if (Files.isRegularFile(entry)){
28+
paths.add(entry.getFileName());
29+
}
30+
31+
}
32+
}
33+
34+
return paths;
35+
}
36+
37+
38+
public static void main(String[] args) throws IOException {
39+
40+
Path myPath = Paths.get("D:\\folder");
41+
42+
43+
List<Path> paths = walk(myPath);
44+
45+
46+
47+
48+
paths.forEach(path -> System.out.println(path));
49+
}
50+
}

0 commit comments

Comments
 (0)