How to list files in a directory in Java

[CG]BOUGA
2,254 views
undefined

Open Source Your Knowledge, Become a Contributor

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

Create Content
// {...}
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