[Java] File class recursively gets all files/folders under directory

Source: Internet
Author: User

 PackageCom.xiwi;ImportJava.io.*;ImportJava.util.*;classfile{ Public Static voidMain (String args[]) {System.out.println ("File Go ..."); //change this to the directory path you want to traverse.Recursivefiles ("F:\\filetext"); System.out.println ("File End."); }    /*** Xiwi * Traverse files/Folders-functions * [String]path file path*/    Private Static voidrecursivefiles (String path) {//Create a File objectFile File =NewFile (path); //fetch files/foldersFile files[] =File.listfiles (); //object is empty direct return        if(Files = =NULL){            return; }                        //file under directory        if(Files.length = = 0) {System.out.println (path+ "No files under this folder"); }                //existence of file traversal judgment         for(File f:files) {//determine if a folder            if(F.isdirectory ()) {System.out.print ("Folder:");                                  System.out.println (F.getabsolutepath ()); //continue traversal for a folderRecursivefiles (F.getabsolutepath ()); //determine if the file is}Else if(F.isfile ()) {System.out.print ("File:");                              System.out.println (F.getabsolutepath ()); } Else{System.out.print ("Unknown error file"); }                    }            }    }

[Java] File class recursively gets all files/folders under 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.