recursive query Specifies the file path under the specified directory
I wrote a simple little way to recursively find the files that I want in the specified directory, including all the files under the directory and subdirectories.
package;
Import Java.io.File;
Import java.util.ArrayList;
Import java.util.List; /** * @author Guan Shijie * @since 2016.9.28 * */public class Researchfile {/** find the absolute path to the specified file based on the file name and parent directory path * @param Filena Me * @param filesaverootpath * @return File absolute path * @throws businessexception * @author Guan/public String FINDF
Ilepath (String fileName, String filesaverootpath) {list<string> pathlist = new arraylist<string> ();
Researchfile (New File (Filesaverootpath), filename,pathlist);
String path = null;
if (Pathlist.size () >=1) path = pathlist.get (0);
return path; /** finds the absolute path to all of the files based on the file name and ancestor directory * @param file * @param fileName * @param pathlist * @author Guan/Public
static void Researchfile (File file,string filename,list<string> pathlist) {if (File.isdirectory ()) {
file[] Filearry = File.listfiles ();
for (File F:filearry) {researchfile (f,filename,pathlist); }
}else{if (File.getname (). Equals (FileName)) {Pathlist.add (File.getabsolutepath ());
}
}
}
}