1 /*2 requirement: Output the absolute path of all files ending in. java in the C:\Users\Administrator\Desktop\ record directory to the console3 Analysis:4 A: Encapsulate the directory5 B: Get file array for all files or folders in this directory6 C: Traverse the file array to get each file object7 D: Determine if the file object being traversed is a folder:8 yes: Go back to B and continue with the execution9 No: Judging whether to end with. JavaTen Yes: Output the absolute path of the file One No: Ignore A Summary: In the above analysis, if it is done by hand. Then, the B, C, D two steps can be set as a recursive method, the recursive method: - return type: void - parameter list: File xxx the export: End With. Java - */ - PackageZl_digui; - + ImportJava.io.File; - Public classFilepathdemo { + A Public Static voidMain (string[] args) { at //Encapsulate This directory -File Allfile =NewFile ("c:\\users\\administrator\\desktop\\ record"); - - //Call Recursion - Getjavafile (allfile); - } in //establishing recursive methods - Private Static voidgetjavafile (File allfile) { to + //get file array for all files or folders in this directory -file[] Arryfile =allfile.listfiles (); the * //iterate over the file array $ for(File file:arryfile) {Panax Notoginseng - //determine if it is a folder the if(File.isdirectory ()) { + //if so, continue to use this folder as a directory to get a file array of files or folders inside it. A //That is, return to step b . the getjavafile (file); + } - Else{ $ //if it is not a folder, but a file, decide whether to end with. Java $ if(File.getname (). EndsWith (". Java")){ - //Yes, then output its absolute path. - System.out.println (File.getabsolutepath ()); the } - }Wuyi } the - } Wu -}
Java 20-2 recursively finding specific format files in a specific directory