1 // ListFiles.java
2
3 import java.io.IOException;
4 import java.nio.file.DirectoryStream;
5 import java.nio.file.Files;
6 import java.nio.file.Path;
7 import java.nio.file.Paths;
8
9 /** 10 * ListFiles zeigt den Umgang mit Verzeichnissen. 11 * @author H.Drachenfels 12 * @version 10.1.2019 13 */
14 public final class ListFiles {
15 private ListFiles() { }
16
17 /** 18 * Listet Verzeichnisse mit ihren Unterverzeichnissen auf. 19 * @param args Verzeichnisnamen 20 * @throws IOException wegen Files.newDirectoryStream(Path) 21 */
22 public static void main(String[] args) throws IOException {
23 for (String s : args) {
24 Path p = Paths.get(s);
25 if (Files.exists(p)) {
26 list(p);
27 }
28 }
29 }
30
31 private static void list(Path p) throws IOException {
32 System.out.println(p);
33
34 if (Files.isDirectory(p)) {
35 try (
36 DirectoryStream<Path> d = Files.newDirectoryStream(p);
37 ) {
38 for (Path entry : d) {
39 list(entry);
40 }
41 }
42 }
43 }
44 }
45