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;
}
}
Read here: https://pt.meta.stackoverflow.com/a/5485/28595
– user28595
The code should be pasted here and not on external link, preferably.
– user28595
Right, next post I will put. Thank you
– Joel Henrique Silva Santos