All file names in the Java scan folder.

Source: Internet
Author: User

All file names in the Java scan folder.

MIS content project system, based on document Resource Management, needs to scan all the files under a folder, the required code implementation.

Package q. test. filepath; import java. io. file; import java. util. arrayList; import java. util. using list; import q. test. filepath. exception. scanFilesException;/*** @ author */public class folderfile={ private static ArrayList <Object> scanFiles = new ArrayList <Object> (); /** implement the secure list **/private static secure list <File> queueFiles = new secure list <File> ();/*** TODO: recursively scans the specified files in the specified folder *@ Return ArrayList <Object> * @ author * @ time November 3, 2017 */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. ^_^... cmd_^ ");} if (directory. isDirectory () {File [] filelist = directory. listFiles (); for (int I = 0; I <filelist. length; I ++) {/** if it is a folder, go to the recursive scan folder **/if (filelist [I]. isDirectory () {// absolute path name string dirctorys. add (filelist [I]. getAbsolutePath ()); /** Recursively scan the following folder **/** scanFilesWithRecursion ** (filelist [I]. getAbsolutePath ();}/** non-Folder **/else {scanFiles. add (filelist [I]. getAbsolutePath () ;}} return scanFiles;}/***** TODO: scan all files under a specified folder in non-recursive mode * @ return ArrayList <Object> * @ param folderPath: folder path to be scanned * @ author * @ time November 3, 2017 */public static ArrayList <Object> scanFilesWithNoRecursion (String folderPath) throws ScanFilesException {F Ile 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. ^_^... pai_^ ");} else {// first scan the first directory again File [] files = directory. listFiles (); // traverse the scanned file array. If it is a folder, put it into the sorted list 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 in scanFiles for scan Files. add (files [I]. getAbsolutePath () ;}// if the program list is not empty, traverse the program list while (! QueueFiles. isEmpty () {// remove the first File headDirectory = queueFiles from the queue list. 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 the queue list queueFiles. add (currentFiles [j]);} else {scanFiles. add (currentFiles [j]. getAbsolutePath () ;}}} return scanFiles ;}}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.