Answers for "Files.list() – Iterate over all files and sub-directories"

0

Files.list() – Iterate over all files and sub-directories

import java.io.File;
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 IterateDirectoryExample {
 
    public static void main(String[] args) {
 
        String dirLocation = "C:/temp";
 
        try {
            List<File> files = Files.list(Paths.get(dirLocation))
                        .map(Path::toFile)
                        .collect(Collectors.toList());
             
            files.forEach(System.out::println);
        } catch (IOException e) {
            // Error while reading the directory
        }
    }
}
Posted by: Guest on September-21-2021

Code answers related to "Files.list() – Iterate over all files and sub-directories"

Browse Popular Code Answers by Language