The MIS content project system is based on document resource management and management. It requires scanning all files under a folder and implementing the required code.
package q.test.filescanner; import java.io.File; import java.util.ArrayList; import java.util.LinkedList; import q.test.filescanner.exception.ScanFilesException; /** * @author */ 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 * @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()){ //Absolute path name string directorys.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 folder path that needs to be scanned* @author * @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.