Read all the txt files in a folder

2

I'm trying to list in my project all the files I've saved in the program before in the Query folder, but it does not only show the files, it also shows their respective paths. How do I make this function more organized only with the names of the txt files and who knows even a method to search by name?

File arquivo; 
Files.walk(Paths.get("C:\Users\Dhyego\Dropbox\Projeto Software\Main\Consultas")).forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
    System.out.println(filePath);
}

output:

    
asked by anonymous 21.06.2016 / 15:47

1 answer

7

Simple, using the getFileName of Path .

As Files.Walk returns a collection of Path's , then the filePath object within forEach is an instance of Path .

Files.walk(Paths.get("C:\Users\Dhyego\Dropbox\Projeto Software\Main\Consultas")).forEach(filePath -> {
    if (Files.isRegularFile(filePath)) {
        System.out.println(filePath.getFileName().toString());
    }
});
    
21.06.2016 / 15:55