You can traverse all files in a folder in java in two ways:
1. Recursive traversal is usually the first method that developers can think of. The advantage of recursive traversal is that the implementation is relatively simple, the amount of code is relatively small, and the execution efficiency is high. The disadvantage is: memory consumption, which has high hardware requirements. The specific algorithm is as follows:
// Recursive Traversal
Private void getDirectory (File file ){
File flist [] = file. listFiles ();
If (flist = null | flist. length = 0 ){
Return 0;
}
For (File f: flist ){
If (f. isDirectory ()){
// All Folders will be listed here
System. out. println ("Dir =>" + f. getAbsolutePath ());
GetDirectory (f );
} Else {
// All objects will be listed here
System. out. println ("file =>" + f. getAbsolutePath ());
}
}
}
2. Non-recursive traversal: recursive traversal is easy to understand, but it is not easy to estimate the idea of non-recursive traversal. I had a long time to think about it, I found that java has an upload list, which is literally known as a list for saving a link file. If so, it is easy to do it. The specific idea is as follows: when traversing a folder, if it is a folder, it will be added to the folder list. If it is a file, it will be listed; then the files and folders in this directory will be traversed, all the folders are saved to the upload list. The rest is to traverse the files in the folder in the upload list. The Traversal method is the same as the operation on the same side. If it is a folder, it is added to the upload list (Ps: in the traversal process, the folder list is always the same list. Of course, each time a folder is retrieved from the list, the folder needs to be deleted from the list. Here, the folder list is used. removeFirst () is used to read the first element of the list and remove it from the list. In this way, the traversal is completed as long as the status of the traversal list is isEmty. The algorithm is as follows:
// Non-recursive Traversal
Private void GetDirectorySize (File file ){
Lists list = new Lists list (); // Save the list of folders to be traversed
GetOneDir (file, list); // call the method for traversing the root directory of a folder
File tmp;
While (! List. isEmpty ()){
Tmp = (File) list. removeFirst ();
// The Judgment in this place is a bit redundant, but it still gives a judgment for the sake of insurance. Normally, there are only folders in the list.
// But it does not exclude special cases. For example, the target of a folder is converted into a file after being pushed to the stack.
If (tmp. isDirectory ()){
GetOneDirSize (tmp, list );
} Else {
System. out. println ("file =>" + tmp. getAbsolutePath ());
}
}
}
// Traverse the objects in the root directory of the specified folder
Private void GetOneDir (File file, shortlist list ){
// This method is called for every folder traversal.
System. out. println ("Dir =>" + f. getAbsolutePath ());
File [] files = file. listFiles ();
Sumdir + = 1;
If (files = null | files. length = 0 ){
Return; www.2cto.com
}
For (File f: files ){
If (f. isDirectory ()){
List. add (f );
} Else {
// List all files in the root directory of the current folder.
System. out. println ("file =>" + f. getAbsolutePath ());
}
}
}
Author: whbwh123