Java uses filefilter to filter files

Source: Internet
Author: User
Package cn.com; import Java. io. file; import Java. io. filefilter; import Java. util. arraylist; import Java. util. list; // Problem description: count the number of files that meet the conditions in a folder // 1 use filefilter to filter out files that meet the conditions // 2 if (file. isdirectory () | (file. isfile () & file. getname (). contains (keyword) // when the file is a directory or the file name contains a keyword, true is returned // 2.1 the file name contains the keyword, of course, one of the results // 2.2 if it is a directory, you need to call this method recursively later. // 3 use public class filetest {static int filescount = 0 recursively; static int folderscount = 0; List <File> filesarraylist = new arraylist <File> (); public static void main (string [] ARGs) {filetest = new filetest (); file folder = new file ("F: \ te "); string keyword = "test"; file [] filesresult = filetest. getfiles (folder, keyword); system. out. println ("A total of directories under this directory:" + folderscount + ""); system. out. println ("A total of files under this directory:" + filescount + ""); system. out. println ("Total files meeting the conditions:" + filesresult. length + "");} public file [] getfiles (file folder, final string keyword) {// 1 obtain the Directory and file [] filterfiles = folder. listfiles (New filefilter () {@ overridepublic Boolean accept (File file) {If (file. isfile () {filescount ++;} If (file. isdirectory () {folderscount ++;} If (file. isdirectory () | (file. isfile () & file. getname (). contains (keyword) {return true;} else {return false ;}}}); // 2 Add the file in the result 1 to the set and use the directory to iterate for (INT I = 0; I <filterfiles. length; I ++) {If (filterfiles [I]. isfile () {filesarraylist. add (filterfiles [I]);} If (filterfiles [I]. isdirectory () {getfiles (filterfiles [I], keyword) ;}}// 3 returned results file finalfiles [] = new file [filesarraylist. size ()]; filesarraylist. toarray (finalfiles); Return finalfiles ;}}

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.