Or you should use the concept of a recursive file search until you find: Here is the code:
String name; //to hold the search file name public String listFolder(File dir) { int flag; File[] subDirs = dir.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory(); } }); System.out.println("File of Directory: " + dir.getAbsolutePath()); flag = Listfile(dir); if (flag == 0) { System.out.println("File Found in THe Directory: " + dir.getAbsolutePath()); Speak("File Found in THe Directory: !!" + dir.getAbsolutePath()); return dir.getAbsolutePath(); } for (File folder : subDirs) { listFolder(folder); } return null; } private int Listfile(File dir) { boolean ch = false; File[] files = dir.listFiles(); for (File file : files) { Listfile(file); if (file.getName().indexOf(name.toLowerCase()) != -1) {//check all in lower case System.out.println(name + "Found Sucessfully!!"); ch = true; } } if (ch) { return 1; } else { return 0; } }
source share