Java Recursive Traversal directory

Source: Internet
Author: User

Java Recursive Traversal directory

First, the content

  Output absolute path to all files (including directories) under the specified directory (folder)

Second, source code: Recursivelistdirectory.java

1  Packagecn.com.zfc.day016;2 3 ImportJava.io.File;4 5 /**6 * @describe recursively traverse the directory7  * @authorZFC8 * @date January 1, 2018 morning 8:44:559  */Ten  Public classRecursivelistdirectory { One  A      Public Static voidMain (string[] args) { -String directoryname = "F:\\studysoft"; -         //1. Mapping Directory Files theFile directory =NewFile (directoryname); -         //2. Call Method - recursivelistdirectory (directory); -     } +  -     /** + * Output The name of the file under the specified directory, including sub-directories A      *  at      * @paramdirectoryname: Directory name -      * @authorZFC - * @date January 1, 2018 morning 8:46:08 -      * -      */ -      Public Static voidrecursivelistdirectory (File directory) { in         //1. Does the mapping directory file exist?  -         if(!directory.exists ()) { to             //does not exist then returns directly +             return; -         } the         //2. Is it a catalogue?  *         if(!directory.isdirectory ()) { $             //not a directory, to determine whether it is a file? Panax Notoginseng             if(Directory.isfile ()) { -System.out.println ("File absolute path:" +Directory.getabsolutepath ()); the             } +}Else { A             //is the directory that gets all the files (including directories) under the directory thefile[] Files =directory.listfiles (); +             //determine if files are empty?  -             if(NULL!=files) { $                 //iterating through an array of files $                  for(File f:files) { -                     //determine if it is a directory?  -                     if(F.isdirectory ()) { the                         //is the directory -System.out.println ("Directory absolute path:" +F.getabsolutepath ());Wuyi recursivelistdirectory (f); the}Else { -                         //not a directory, to determine whether it is a file?  Wu                         if(F.isfile ()) { -System.out.println ("File absolute path:" +F.getabsolutepath ()); About                         } $                     } -                 } -             } -         } A     } +}

Iii. Results of operation

Java Recursive Traversal directory

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.