How to list files in a directory in Java

[CG]BOUGA
1,113 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content
1
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// {...}
Path directory = Paths.get("/");
// Method 1: get the files in the given directory
Files.list(directory)
.filter(Files::isDirectory)
.forEach(System.out::println);
// Method 2: recursively run through the tree (at a maximum depth of 2)
Files.walk(directory, 2)
.filter(Files::isRegularFile)
.forEach(System.out::println);
//{...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content