Java Folder Read Recursive getFilesParent(File dir, String excludeFiles, boolean includeParentDir, boolean includeFiles, boolean includeDirs, boolean recursive)

Here you can find the source of getFilesParent(File dir, String excludeFiles, boolean includeParentDir, boolean includeFiles, boolean includeDirs, boolean recursive)

Description

This is the initial parent to getFiles.

License

Open Source License

Parameter

Parameter Description
dir a parameter
excludeFiles a parameter
includeParentDir a parameter
includeFiles a parameter
includeDirs a parameter
recursive a parameter

Declaration

public static File[] getFilesParent(File dir, String excludeFiles, boolean includeParentDir,
        boolean includeFiles, boolean includeDirs, boolean recursive) 

Method Source Code

//package com.java2s;
/**//from   w  w w .java2 s  .c  om
 * (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.ArrayList;

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

public class Main {
    /**
     * This is the initial parent to getFiles.  This envelope allows the user to decide whether the initial parent folder should be
     * included in the list or not.
     * 
     * @param dir
     * @param excludeFiles
     * @param includeParentDir
     * @param includeFiles
     * @param includeDirs
     * @param recursive
     * @return
     */
    public static File[] getFilesParent(File dir, String excludeFiles, boolean includeParentDir,
            boolean includeFiles, boolean includeDirs, boolean recursive) {

        File[] files = null;
        List<File> filelist = new ArrayList<File>();
        filelist = getFiles(dir, excludeFiles, includeFiles, includeDirs, recursive, filelist);

        if (includeParentDir) {
            files = new File[filelist.size() + 1];
            files[0] = dir;
            for (int i = 0; i < filelist.size(); i++) {
                files[i + 1] = filelist.get(i);
            }
        } else {
            files = new File[filelist.size()];
            for (int i = 0; i < filelist.size(); i++) {
                files[i] = filelist.get(i);
            }
        }

        return files;
    }

    /**
     * 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. getFilesAsArrayListRecursive(String path)
  3. getFilesAsArrayListRecursiveHelper(File f, ArrayList filelist)
  4. getFilesByNameRecursive(Collection filesList, File path, String name)
  5. getFilesForFolder(File folder, boolean recursive)