Java 20-2 recursively finding specific format files in a specific directory

Source: Internet
Author: User

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

Related Article

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.