Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the only file names from the folder in java8

Tags:

java

file

java-8

I am want to list out the only file names from the folder in Java 8. I have tried this code, but it is giving me the complete path.

try {
    List<java.nio.file.Path> files      = Files.list(new   File("F://csv/").toPath())
                .filter(p -> !p.getFileName()
                .toString().startsWith("."))
                .limit(3)
                .collect(Collectors.toList());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
like image 221
Bodapati Srinu Avatar asked Dec 04 '25 17:12

Bodapati Srinu


1 Answers

Use Path::getFileName to get the file name from a path:

import static java.util.stream.Collectors.toList;

List<Path> fileNames = Files.list(Paths.get("f:/csv"))
                             .filter(...)
                             .limit(...)
                             .map(Path::getFileName)
                             .collect(toList());
like image 69
Misha Avatar answered Dec 06 '25 10:12

Misha