I have used recursion to search for particular type of file (for example .pdf files is used here). My recursion algorithm searches for all subfolder. However I found that it lacks performance when there is too many sub-folder. sub-sub-folder, sub-sub-sub-folder. I want to know if there is better algorithm for file searching.
Below is my recursion code for file searching. I have used .pdf file as an example
import java.io.File;
public class FInd {
public static void main(String[] args) {
File f = new File("D:/");
find(f);
}
public static void find(File f){
File []list = f.listFiles();
try{
for(int i=0;i<list.length && list.length>0;i++){
if(list[i].isFile() && (list[i].getName().contains(".pdf")) ||
list[i].getName().contains(".PDF"))
System.out.println(list[i].getAbsolutePath());
if(list[i].isDirectory()) find(list[i]);
}
}catch(Exception e){
}
}
}
This code is somewhat faster or equal to when compared to search option in file explorer. I want to know any faster algorithm than this