Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add all files recursively from root with Java 8 Stream

I have the following recursive method that simply adds all the child items in a given folder to a list:

private List<TemplateFile> readTemplateFiles(String nextTemplateDir, String rootTemplateDir) throws FileNotFoundException {
    List<TemplateFile> templateFiles = new ArrayList<>();

    for (File file : new File(nextTemplateDir).listFiles()) {
        if (!file.isDirectory() && !file.getName().startsWith(".")) {
            templateFiles.add(TemplateFile.create(file, rootTemplateDir));
        } else if (file.isDirectory()) {
            templateFiles.addAll(readTemplateFiles(file.getAbsolutePath(), rootTemplateDir));
        }
    }

    return templateFiles;
}

How could I refactor this method using the new Java 8 Stream API?

like image 516
S-K' Avatar asked Jan 04 '16 09:01

S-K'


People also ask

What are Java 8 streams?

Streams, introduced in Java 8, use functional-style operations to process data declaratively. The elements of streams are consumed from data sources such as collections, arrays, or I/O resources like files. In this article, we’ll explore the various possibilities of using streams to make life easier when it comes to the handling of files.

How do I Walk a file in Java 8?

Using Java 8 Java 8 introduced a method walk () in Files class. Return a Stream that is lazily populated with Path by walking the file tree rooted at a given starting file. The file tree is traversed depth-first, the elements in the stream are Path objects that are obtained as if by resolving the relative path against start.

How do I list all files in a directory in Java 8?

Java 8 – List of All Files in a Directory Learn to use Java 8 APIs such as Files.list () and DirectoryStream to list all files present in a directory, including hidden files, recursively. For using external iteration (for loop) use DirectoryStream. For using Stream API operations (map, filter, sorted, collect), use Files.list () instead.

How to iterate over all files in a directory in Java?

Files.list () – Iterate over all files and sub-directories Java example to iterate over a directory and get the List<File> using Files.list () method. 2. Files.list () – List only files excluding sub-directories Use the filter expression Files::isRegularFile to check if a file is normal file, and not any directory.


1 Answers

You could use Files.walk(start, options...) to walk through a file tree recursively. This method returns a Stream<Path> consisting of all the Path starting from the given root.

Return a Stream that is lazily populated with Path by walking the file tree rooted at a given starting file. The file tree is traversed depth-first, the elements in the stream are Path objects that are obtained as if by resolving the relative path against start.

private List<TemplateFile> readTemplateFiles(String nextTemplateDir, String rootTemplateDir) throws FileNotFoundException {
    return Files.walk(Paths.get(nextTemplateDir))
                .filter(path -> !path.getFileName().startsWith("."))
                .map(path -> TemplateFile.create(path.toFile(), rootTemplateDir))
                .collect(Collectors.toList());
}

Among the options, there is FOLLOW_LINKS that will follow symbolic links.

like image 186
Tunaki Avatar answered Sep 25 '22 00:09

Tunaki