Recursively queries the specified file name under All files and subfolders under the specified directory __ recursive

Source: Internet
Author: User
Tags parent directory
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 ());
 }
        }
    }  
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.