In this example, let’s see how to traverse a folder using Files.walk.
Source Code
package com.beginner.examples;
import java.nio.file.*;
public class FilesWalkExample {
public static void main(String[] args){
// traverse the folder
try
{
// traverse the folder
Stream path = Files.walk(Paths.get("D:\example"));
List files = path.filter(Files::isRegularFile)
.map(x -> x.toString()).collect(Collectors.toList());
// print files
files.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
test.txt
test.md
example.txt