I'm trying to list all the files in the C: \ directory of my PC. Smaller directories with 11,000 files are picking up normally, it takes a while, but it's no problem. The problem is when I will list the C: \ files, because it is the root folder and have at least 1 million files and my method is using recursion 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;
}
}