Getting only file names from the folder in Java8 with Filtering

Viewed 8222

I want to list out only file names from the folder in Java 8 ending with .txt extension.

With this piece of code I got all the files names, and there are a lot with .txt extensions:

List<Path> fileNames = Files.list(configFilePath)
                .map(Path::getFileName)
                .sorted()
                .collect(toList());

But with this code I get an empty:

List<Path> fileNames = Files.list(configFilePath)
                filter(file -> file.getFileName().endsWith(".txt"))
                .map(Path::getFileName)
                .sorted()
                .collect(toList());
1 Answers
Related