Java Folder Read Recursive getFiles(File dir, String excludeFiles, boolean includeFiles, boolean includeDirs, boolean recursive, List filelist)

Here you can find the source of getFiles(File dir, String excludeFiles, boolean includeFiles, boolean includeDirs, boolean recursive, List filelist)

Description

Recursive function to descend into the directory tree and find all the files and directories excluding those specified by exclude.

License

Open Source License

Parameter

Parameter Description
dir A file object defining the top directory
excludeFiles a parameter
includeFiles a parameter
includeDirs a parameter
recursive a parameter
filelist a parameter

Declaration

public static List<File> getFiles(File dir, String excludeFiles, boolean includeFiles, boolean includeDirs,
        boolean recursive, List<File> filelist) 

Method Source Code

//package com.java2s;
/**//from   w w  w  . j  av  a2s  . co  m
 * (c) 2014 Cisco and/or its affiliates. All rights reserved.
 * 
 * This software is released under the Eclipse Public License. The details can be found in the file LICENSE. 
 * Any dependent libraries supplied by third parties are provided under their own open source licenses as 
 * described in their own LICENSE files, generally named .LICENSE.txt. The libraries supplied by Cisco as 
 * part of the Composite Information Server/Cisco Data Virtualization Server, particularly csadmin-XXXX.jar, 
 * csarchive-XXXX.jar, csbase-XXXX.jar, csclient-XXXX.jar, cscommon-XXXX.jar, csext-XXXX.jar, csjdbc-XXXX.jar, 
 * csserverutil-XXXX.jar, csserver-XXXX.jar, cswebapi-XXXX.jar, and customproc-XXXX.jar (where -XXXX is an 
 * optional version number) are provided as a convenience, but are covered under the licensing for the 
 * Composite Information Server/Cisco Data Virtualization Server. They cannot be used in any way except 
 * through a valid license for that product.
 * 
 * This software is released AS-IS!. Support for this software is not covered by standard maintenance agreements with Cisco. 
 * Any support for this software by Cisco would be covered by paid consulting agreements, and would be billable work.
 * 
 */

import java.io.File;

import java.util.List;
import java.util.StringTokenizer;

public class Main {
    /**
     * Recursive function to descend into the directory tree and find all the files and directories excluding those specified by exclude.
     * 
     * @param dir A file object defining the top directory
     * @param excludeFiles
     * @param includeFiles
     * @param includeDirs
     * @param recursive
     * @param filelist
     * @return
     */
    public static List<File> getFiles(File dir, String excludeFiles, boolean includeFiles, boolean includeDirs,
            boolean recursive, List<File> filelist) {

        File listFile[] = dir.listFiles();
        if (listFile != null) {
            for (int i = 0; i < listFile.length; i++) {
                if (!excludeFile(listFile[i].getName(), excludeFiles)) {
                    if (listFile[i].isDirectory()) {
                        if (includeDirs) {
                            filelist.add(listFile[i]);
                        }
                        if (recursive) {
                            filelist = getFiles(listFile[i], excludeFiles, includeFiles, includeDirs, recursive,
                                    filelist);
                        }
                    } else {
                        if (includeFiles)
                            filelist.add(listFile[i]);
                    }
                }
            }
        }
        return filelist;
    }

    /**
     * Determine whether a filename should be excluded from a list by verifying it against the excludeFiles comma separated list.
     *  
     * @param path
     * @param excludeFiles
     * @return
     */
    public static boolean excludeFile(String filename, String excludeFiles) {
        boolean exclude = false;

        StringTokenizer st = new StringTokenizer(excludeFiles, ",");
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            if (filename.endsWith(token)) {
                exclude = true;
                break;
            }
        }

        return exclude;
    }
}

Related

  1. getFiles(File directory, boolean recursive)
  2. getFiles(File directory, boolean recursive)
  3. getFilesAsArrayListRecursive(String path)
  4. getFilesAsArrayListRecursiveHelper(File f, ArrayList filelist)