Java scans all files under the specified folder for your reference. The specific content is as follows
Scan all files under a folder, because there is no limit on the number of layers in the folder, which may be as many as dozens or hundreds of layers. Usually, there are two ways to traverse all files under the specified folder.
Below I will give two implementation codes, including recursive and non-recursive implementations, the 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 Evil Mr.*/ public class FolderFileScanner { private static ArrayList<Object> scanFiles = new ArrayList<Object>(); /** linkedList implementation**/ private static LinkedList<File> queueFiles = new LinkedList<File>(); /** * TODO: Recursively scan the specified file under the specified folder* @return ArrayList<Object> * @author Mr. Evil (LQ) * @time November 3, 2017*/ public static ArrayList<Object> scanFilesWithRecursion(String folderPath) throws ScanFilesException{ ArrayList<String> directories = 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, enter the recursive scan folder**/ if(filelist[i].isDirectory()){ directories.add(filelist[i].getAbsolutePath()); /**Recursively scan the folder below**/ scanFilesWithRecursion(filelist[i].getAbsolutePath()); } /** Non-folder**/ else{ scanFiles.add(filelist[i].getAbsolutePath()); } } } return scanFiles; } /** * * TODO: Scan all files below the specified folder in a non-recursive manner* @return ArrayList<Object> * @param folderPath The path of the folder that needs to be scanned* @author Mr. Evil (LQ) * @time November 3, 2017*/ 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 scan the first layer directory File [] files = directory.listFiles(); //Transulate the scanned file array. If it is a folder, put it into linkedList and process it later for(int i = 0; i < files.length; i ++){ if(files[i].isDirectory()){ queueFiles.add(files[i]); }else{ //Temporarily put the file name into scanFiles scanFiles.add(files[i].getAbsolutePath()); } } //If linkedList is non-empty traversed linkedList while(!queueFiles.isEmpty()){ //Move out the first File in linkedList headDirectory = queueFiles.removeFirst(); File [] currentFiles = headDirectory.listFiles(); for(int j = 0; j < currentFiles.length; j ++){ if(currentFiles[j].isDirectory()){ //If it is still a folder, put it into linkedList queueFiles.add(currentFiles[j]); }else{ scanFiles.add(currentFiles[j].getAbsolutePath()); } } } return scanFiles; } } } } } return scanFiles; } } } } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.