Read all txt files in a folder

Asked

Viewed 581 times

2

I am trying to list in my project all the files that I saved previously in the program inside the query folder, however it does not only show the files, it also shows their respective paths. How do I make this function more organized only with the name of the txt files and maybe 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);
}

exit:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • What version of java are you using?

  • @adelmo00 Probably Java8, it was not in this version that lambda expressions were implemented?

  • Yes. I was going to answer, but @jbueno already did and I hope it helps you. I didn’t notice the expression lambda at the end of the line.

  • I’m using Version 8

  • 1

    @Carlosdiego you completely changed the question, putting another question, and the question has already been answered. I will return to original doubt, whenever you have a new doubt, do not change the question, accept the answer of the current and open a new.

  • 2

    @Carlosdiego Keep the question as it was before and open a new one with your new doubt. This way you will end up invalidating my answer.

  • Done jbueno > http://answall.com/questions/136281/aber-arquivo-txt-dentro-de-uma-pasta-e-editar-informa%C3%A7%C3%B5es-do-arquivo

Show 2 more comments

1 answer

7


Simple, using the method getFileName of Path.

Like the Files.Walk returns a collection of Path's, logo the object filePath within the 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());
    }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.