Skip to content

Commit 0b7abf1

Browse files
Adding directory listing example
1 parent 90cd0d9 commit 0b7abf1

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.howtodoinjava.io;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
import java.util.List;
9+
import java.util.stream.Collectors;
10+
11+
public class IterateDirectoryExample {
12+
13+
public static void main(String[] args) {
14+
15+
String dirLocation = "C:/temp";
16+
17+
try {
18+
List<File> files = Files.list(Paths.get(dirLocation))
19+
.map(Path::toFile)
20+
.collect(Collectors.toList());
21+
22+
files.forEach(System.out::println);
23+
} catch (IOException e) {
24+
// Error while reading the directory
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)