Recursive and non-recursive methods to get all the files and folders in a folder

Source: Internet
Author: User

/******************************************************* Recursive implementation of *************************************************** /

/**
* Get all files for a folder
* @param filePath Get the folder path of the file
* @param myFiles file collection for saving files
* @return All files and folders
* @throws IOException
*/
public static arraylist<file> Getallfiles (String FilePath,
Arraylist<file> myFiles) throws IOException {//due to recursive invocation of the function itself, use formal parameters to do the return value of the function
File File = new file (FilePath);
if (file.exists ()) {//Determine if the folder exists
For (File fi:file.listFiles ()) {//Traverse folder
if (Fi.isdirectory ()) {
Myfiles.add (FI);//If a folder is added to the collection
Getallfiles (Fi.getcanonicalpath (), myFiles);//If the directory is a recursive call
} else {
Myfiles.add (FI);//If the file is added to the collection
}
}
}
Return myfiles;//returns all files and folders
}

/********************************************************* non-recursive implementation ************************************************ /

/**
* Get all Files under a directory (non-recursive implementation)
* @param path Directory
* @return All files and folders
*/
public static arraylist<file> Getallfilesanddirs (String path) {
File File = new file (path), Tempfile = null;//defines a temporary file variable and initializes it to null based on the path path to the new one
int index = 0;//defines the index of the collection and initializes it to 0
arraylist<file> dirlist = new arraylist<file> ();//define a collection of saved folders
arraylist<file> fileList = new arraylist<file> ();//define a collection of saved files
if (file.exists () && file.isdirectory ()) {//Determine if file is a folder
Dirlist.add (file);//Add File to the folder collection
Tempfile = dirlist.get (0);//Initialize temporary file variable
} else {
Filelist.add (file);//If file is files, add to file collection
}
while (tempfile! = null) {//While loop, exit loop If temp file variable is empty
index++;//Index self-increment
for (file Fi:tempFile.listFiles ()) {//Traversal tempfile temp file variable
if (Fi.isdirectory ()) {
Dirlist.add (FI);//Tempfile a folder exists in the temporary file variable to be added to the folder collection
} else {
Filelist.add (FI);//tempfile files in temporary file variables are added to the file collection
}
}
if (Index < dirlist.size ()) {//To avoid array out-of-bounds, only if the index is less than the collection length, Tempfile re-assigns the folder object to the folder collection
Tempfile = Dirlist.get (index);//Tempfile is re-assigned as the Index folder object of a folder collection
} else {
Tempfile = null;//When the index variable is greater than the folder collection length, the Tempfile assignment is empty, at which time the loop ends
}
}
for (File fi:dirlist) {//Traverse Folder collection
Filelist.add (FI);//Add the Folder object in the folder collection to the File collection
}
Return filelist;//the file collection FileList get all the folders and files back to the collection
}

Recursive and non-recursive methods to get all the files and folders in a folder

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.