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