java recursive and non recursive implementations scan all files in the folder

  • 2021-01-25 07:29:28
  • OfStack

java scans all files under the specified folder for your reference. The details are as follows

Scan all files under a folder, because there is no limit to the number of layers of the folder may be as many as several 10 layers hundreds of layers, usually use two ways to traverse all files under the specified folder.

recursively Non-recursive (queue or stack implementation)

In the following, I will present the implementation code of two ways, including recursive and non-recursive implementation. code is shown below.

java code:


package q.test.filescanner; 
 
import java.io.File; 
import java.util.ArrayList; 
import java.util.LinkedList; 
 
import q.test.filescanner.exception.ScanFilesException; 
 
/** 
 * @author  Mr. Evil  
 */ 
public class FolderFileScanner { 
   
  private static ArrayList<Object> scanFiles = new ArrayList<Object>(); 
   
  /**linkedList implementation **/ 
  private static LinkedList<File> queueFiles = new LinkedList<File>(); 
   
   
  /** 
   * TODO: Recursively scans the specified file under the specified folder  
   * @return ArrayList<Object> 
   * @author  Mr. Evil ( LQ )  
   * @time 2017 years 11 month 3 day  
   */ 
  public static ArrayList<Object> scanFilesWithRecursion(String folderPath) throws ScanFilesException{ 
    ArrayList<String> dirctorys = new ArrayList<String>(); 
    File directory = new File(folderPath); 
    if(!directory.isDirectory()){ 
      throw new ScanFilesException('"' + folderPath + '"' + " input path is not a Directory , please input the right path of the Directory. ^_^...^_^"); 
    } 
    if(directory.isDirectory()){ 
      File [] filelist = directory.listFiles(); 
      for(int i = 0; i < filelist.length; i ++){ 
        /** If it is currently a folder, go to the recursive scan folder **/ 
        if(filelist[i].isDirectory()){ 
          dirctorys.add(filelist[i].getAbsolutePath()); 
          /** Recursively scan the following folders **/ 
          scanFilesWithRecursion(filelist[i].getAbsolutePath()); 
        } 
        /** The folder **/ 
        else{ 
          scanFiles.add(filelist[i].getAbsolutePath()); 
        } 
      } 
    } 
    return scanFiles; 
  } 
   
  /** 
   * 
   * TODO: Non-recursive scan of all files under the specified folder  
   * @return ArrayList<Object> 
   * @param folderPath  The path of the folder to scan the files  
   * @author  Mr. Evil ( LQ )  
   * @time 2017 years 11 month 3 day  
   */ 
  public static ArrayList<Object> scanFilesWithNoRecursion(String folderPath) throws ScanFilesException{ 
    File directory = new File(folderPath); 
    if(!directory.isDirectory()){ 
      throw new ScanFilesException('"' + folderPath + '"' + " input path is not a Directory , please input the right path of the Directory. ^_^...^_^"); 
    } 
    else{ 
      // First of all, will be the first 1 Layer directory scan 1 through  
      File [] files = directory.listFiles(); 
      // Iterate through the array of files, and if it's a folder, put it in linkedList Intermediate later processing  
      for(int i = 0; i < files.length; i ++){ 
        if(files[i].isDirectory()){ 
          queueFiles.add(files[i]); 
        }else{ 
          // Temporarily put the filename in scanFiles In the  
          scanFiles.add(files[i].getAbsolutePath()); 
        } 
      } 
       
      // if linkedList Not empty traversal linkedList 
      while(!queueFiles.isEmpty()){ 
        // Removed from the linkedList The first of 1 a  
        File headDirectory = queueFiles.removeFirst(); 
        File [] currentFiles = headDirectory.listFiles(); 
        for(int j = 0; j < currentFiles.length; j ++){ 
          if(currentFiles[j].isDirectory()){ 
            // If it's still a folder, put it in linkedList In the  
            queueFiles.add(currentFiles[j]); 
          }else{ 
            scanFiles.add(currentFiles[j].getAbsolutePath()); 
          } 
        } 
      } 
    } 
     
    return scanFiles; 
  } 
} 

Related articles: