Find All Files in Directory and Subdirectories using Java

Files.walk method

package app;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

public class Main
{
    public static void main(String[] args) throws IOException
    {
        String rootDir = "test_dir";

        List<String> files = Files.walk(Paths.get(rootDir))
                .filter(Files::isRegularFile)
                .map(Path::toString)
                .collect(Collectors.toList());

        System.out.println(files);
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.