Problem to list directories with java

Asked

Viewed 202 times

0

I’m trying to list all the files in the C: directory on my PC. Smaller directories with 11 thousand files is picking up normally, it takes a while, but it’s no problem. The problem is when I will list the C files: for being the root folder and having at least 1 million files and my method is using recursiveness it give Nullpointer. Is there any way to list C: completely in a practical way?

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class PercorrendoArquivosComSubPasta {


    public static void main(String[] args) {
        ArrayList<File> list = (ArrayList<File>) buscaRecursiva(new File("C:\\Users\\Joel\\Desktop\\"),".txt");
        for(File i : list) {
            System.out.println(i);
        }
    }

    public static List<File> buscaRecursiva(File dir, String extensao) {

        List<File> listFile = new ArrayList<File>();

        for (File f : dir.listFiles()){
           if (f.isDirectory()) {
               listFile.addAll(buscaRecursiva(f, extensao));

           } else if (f.getName().endsWith(extensao)) {
               listFile.add(f);
           } 
        }
        return listFile;
    }

}

CODE IN THE PASTEBIN

  • 2

    Read here: https://pt.meta.stackoverflow.com/a/5485/28595

  • 1

    The code should be pasted here and not on external link, preferably.

  • Right, next post I will put. Thank you

1 answer

0


Method documentation File.listFiles():

Returns null if this Abstract pathname does not denote a directory, or if an I/O error occurs.

ie, returns null if the path does not represent a directory. What is expected to happen in the case of all files that are not directories.

EDIT: I did a test here on Linux and gave NullPointerException when trying to read a folder I was not allowed to read. To do this check in the code just use file.canRead(). Example:

import java.io.File;

public class ListFiles {

    public static void main(String [] args) {
        if (args.length > 0) {
            File file = new File(args[0]);
            listar(file);
        }
    }

    private static void listar(File file) {
        System.out.println(file.getAbsolutePath());
        if (file.isDirectory() && file.canRead()) {
            File[] files = file.listFiles();
            for (File f : files) {
                listar(f);
            }
        }
    }
}
  • Piovezan, is there any method to go through the entire directory along with its sub-folders?

  • I did not suggest question for not having done any test with the code. But it is almost correct, I think it is a matter of checking by null before using an object or testing arquivo.isDirectory() before callingarquivo.listFiles().

  • I would also avoid declaring listFile inside buscaRecursiva(), declare out and pass as parameter for this method.

  • I suggest looking this question (in English) showing several alternatives to list files recursively in Java.

  • Thank you Piovezan, I’ll take a look.

  • Just remembering that in addition to accepting an answer you can also vote +1 on it. Incidentally you can vote +1 on any answer you find useful and correct.

  • Only allowed when you have 15 or more reputations.

Show 2 more comments

Browser other questions tagged

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