Java Folder Visit deepListFiles(File dir, FileFilter filter)

Here you can find the source of deepListFiles(File dir, FileFilter filter)

Description

Return a list of files from the given directory and matching the given filter.

License

LGPL

Parameter

Parameter Description
dir the directory to explore
filter the FileFilter to use (may be null)

Return

a List of java.io.File

Declaration

public static List<File> deepListFiles(File dir, FileFilter filter) 

Method Source Code


//package com.java2s;
/*/*from  w  ww . j  ava2  s .co m*/
 GNU LESSER GENERAL PUBLIC LICENSE
 Version 3, 29 June 2007
    
 Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.
    
    
 This version of the GNU Lesser General Public License incorporates
 the terms and conditions of version 3 of the GNU General Public
 License
 */

import java.io.File;
import java.io.FileFilter;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Return a list of files from the given directory and matching the given
     * filter. If a filter is provided, the directories (and their content) that
     * do not match the filter are skipped.
     * 
     * @param dir
     *          the directory to explore
     * @param filter
     *          the FileFilter to use (may be null)
     * @return a List of java.io.File
     * @see #deepListFiles(File, FileFilter, boolean)
     */
    public static List<File> deepListFiles(File dir, FileFilter filter) {
        return deepListFiles(dir, filter, true);
    }

    /**
     * Return a list of files from the given directory and matching the given
     * filter.
     * 
     * @param dir
     *          the directory to explore
     * @param filter
     *          the FileFilter to use (may be null)
     * @param checkDir
     *          if true the directories that do not match the filter are skipped.
     * @return a List of java.io.File
     */
    public static List<File> deepListFiles(File dir, FileFilter filter, boolean checkDir) {
        ArrayList<File> list = new ArrayList<File>();
        deepListFilesVisitor(dir, filter, checkDir, list);
        return list;
    }

    private static void deepListFilesVisitor(File file, FileFilter filter, boolean checkDir, List<File> list) {
        if (file.isDirectory()) {
            if (checkDir && filter != null && !filter.accept(file)) {
                return;
            }
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                deepListFilesVisitor(files[i], filter, checkDir, list);
            }
        } else {
            if (filter == null || filter.accept(file)) {
                list.add(file);
            }
        }
    }
}

Related

  1. deepListFiles(String sDir, String suffix)
  2. deepListFilesVisitor(File file, FileFilter filter, boolean checkDir, List list)