Java implementation Simple file filter

Source: Internet
Author: User

Enter the path to find a file of the specified file type under this path

Code ideas:

Recursion is used to iterate through all subfolders of the folder.

First determine if the path exists:

Yes: Get file

Determine if the folder:

Yes: Call itself, continue to get sub-folder content

No: Determine the file suffix, match the output

No: Output hint

1  PackageCom.hanqi.maya.util;2 3 ImportJava.io.File;4 ImportJava.util.Scanner;5 6  Public classTestguolv {7      Public Static voidMain (string[] args) {8Scanner sc=NewScanner (system.in);9System.out.println ("Please enter a path to find");TenString s=sc.nextline (); OneFile File =NewFile (s); AFilesum (file,1); -     } -      Public Static voidFilesum (File F,intLen) { the  -         if(F.exists ()) {//determine if a path exists -File[] Files=f.listfiles ();//gets the file name under the path -              for(File fi:files) {//loop output File name +                 if(Fi.isdirectory ()) {//determine if the folder - Printblank (len);  +System.out.println (Fi.getname () + "folder"); AFilesum (fi,len+1);//continue calling itself at}Else{ -Printblank (len);//Output Spaces -                     if(Fi.getname (). EndsWith (". Java")) {//If the file is the ". Java" suffix, then the output - System.out.println (Fi.getname ()); -                     }                     -                 } in             } -}Else{ toSYSTEM.OUT.PRINTLN ("file does not exist!!! "); +         } -     } the      *      Public Static voidPrintblank (intLen) { $          for(inti = 0; i < Len; i++) {//output. To differentiate the folder hierarchyPanax NotoginsengSystem.out.print ("."); -         } the          +     } A}

Summary and questions:

Yesterday, I learned all the files under the output folder, and today I changed it into a file to find the suffix specified under the folder.

And because the output when there is no hierarchy, so write a loop used in the output of the file before adding a space to clear the hierarchy, and then found that the space is not clear enough to the level of Chinese characters, encountered Chinese characters will be indented a few spaces, but the symbol will not be so.

There is also an issue with how to control the specified suffix name is not resolved.

The road has long been repaired.

Java implementation Simple file filter

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.