List directory and subdirectory files with listFiles

2

Today I'm ready through the code below the files that are in the directory passed by the parameter, however now I would like to also list the files that are in the subdirectories. I did some searches but I did not find anything that did not change my structure a lot, I also thought that removing isFile() would solve, but it did not work.

void indexaArquivosDoDiretorio(File raiz) {
    FilenameFilter filtro = new FilenameFilter() {
        public boolean accept(File arquivo, String nome) {
            return nome.toLowerCase().endsWith(".pdf")
                    || nome.toLowerCase().endsWith(".odt")
                    || nome.toLowerCase().endsWith(".doc")
                    || nome.toLowerCase().endsWith(".docx")
                    || nome.toLowerCase().endsWith(".ppt")
                    || nome.toLowerCase().endsWith(".pptx")
                    || nome.toLowerCase().endsWith(".xls")
                    || nome.toLowerCase().endsWith(".txt")
                    || nome.toLowerCase().endsWith(".rtf");
        }
    };

    for (File arquivo : raiz.listFiles(filtro)) {
        if (arquivo.isFile()) {
            try {
                // Extrai o conteúdo do arquivo com o Tika;
                String textoExtraido = getTika().parseToString(arquivo);
                indexaArquivo(arquivo, textoExtraido);
                System.out.println(arquivo);
                i++;
            } catch (Exception e) {
                logger.error(e);
            }
        } else {
            indexaArquivosDoDiretorio(arquivo);
        }
    }
}
    
asked by anonymous 11.06.2014 / 16:00

2 answers

3

You can browse a structure of directories, subdirectories, and files with the method:

Path walkFileTree(Path start, FileVisitor<? super Path> visitor)

You need to specify the home directory and implement the FileVisitor interface to tell you exactly what information to display when you browse the directory or file in question.

An example:

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

class MyFileVisitor extends SimpleFileVisitor<Path> {
    public FileVisitResult visitFile(Path path, BasicFileAttributes fileAttributes){
        System.out.println("Nome do arquivo:" + path.getFileName());
        return FileVisitResult.CONTINUE;
    }
    public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes fileAttributes){
        System.out.println("----------Nome do diretório:" + path + "----------");
        return FileVisitResult.CONTINUE;
    }
}

public class Walk {
    public static void main(String[] args) {
        Path source = Paths.get("C:\Users\Math\Desktop");
        try {
            Files.walkFileTree(source, new MyFileVisitor());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Source: Oracle Certified Professional Java SE 7 Programmer Exams 1Z0-804 and 1Z0-805: A Comprehensive OCPJP 7 Certification Guide ( Expert's Voice in Java)

    
11.06.2014 / 16:19
1

I was able to solve my difficulty as follows: As I was already using a filter to show me the kind of files I was interested in, I came to the conclusion that the right place to check would be there. Then it came to my mind that a directory has no extension, so it would end with "empty" the name, and that's what I did. I added the nome.toLowerCase().endsWith("") line to my FilenameFilter and it worked.

The following code was used:

void indexaArquivosDoDiretorio(File raiz) {
        FilenameFilter filtro = new FilenameFilter() {
            public boolean accept(File arquivo, String nome) {
                return nome.toLowerCase().endsWith(".pdf")
                        || nome.toLowerCase().endsWith(".odt")
                        || nome.toLowerCase().endsWith(".doc")
                        || nome.toLowerCase().endsWith(".docx")
                        || nome.toLowerCase().endsWith(".ppt")
                        || nome.toLowerCase().endsWith(".pptx")
                        || nome.toLowerCase().endsWith(".xls")
                        || nome.toLowerCase().endsWith(".txt")
                        || nome.toLowerCase().endsWith(".rtf")
                        || nome.toLowerCase().endsWith("");
            }
        };

    for (File arquivo : raiz.listFiles(filtro)) {
        if (arquivo.isFile()) {
            try {
                // Extrai o conteúdo do arquivo com o Tika;
                String textoExtraido = getTika().parseToString(arquivo);
                indexaArquivo(arquivo, textoExtraido);
                System.out.println(arquivo);
                i++;
            } catch (Exception e) {
                logger.error(e);
            }
        } else {
            indexaArquivosDoDiretorio(arquivo);
        }
    }
}
    
12.06.2014 / 15:24