Java Folder Read by Filter getFiles(String name, FileFilter filter, boolean includeSubtree)

Here you can find the source of getFiles(String name, FileFilter filter, boolean includeSubtree)

Description

Returns a list of all filtered files in the RFS.

If the name is not a folder the folder that contains the given file will be used instead.

Despite the filter may not accept folders, every subfolder is traversed if the includeSubtree parameter is set.

License

Open Source License

Parameter

Parameter Description
name a folder or file name
filter a filter
includeSubtree if to include subfolders

Return

a list of filtered objects

Declaration

public static List<File> getFiles(String name, FileFilter filter, boolean includeSubtree) 

Method Source Code

//package com.java2s;
/*/*from   w  w w . j a va 2 s . c om*/
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

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

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Returns a list of all filtered files in the RFS.<p>
     * 
     * If the <code>name</code> is not a folder the folder that contains the
     * given file will be used instead.<p>
     * 
     * Despite the filter may not accept folders, every subfolder is traversed
     * if the <code>includeSubtree</code> parameter is set.<p>
     * 
     * @param name a folder or file name
     * @param filter a filter
     * @param includeSubtree if to include subfolders
     * 
     * @return a list of filtered <code>{@link File}</code> objects
     */
    public static List<File> getFiles(String name, FileFilter filter, boolean includeSubtree) {

        List<File> ret = new ArrayList<File>();

        File file = new File(name);
        if (!file.isDirectory()) {
            file = new File(file.getParent());
            if (!file.isDirectory()) {
                return ret;
            }
        }
        File[] dirContent = file.listFiles();
        for (int i = 0; i < dirContent.length; i++) {
            File f = dirContent[i];
            if (filter.accept(f)) {
                ret.add(f);
            }
            if (includeSubtree && f.isDirectory()) {
                ret.addAll(getFiles(f.getAbsolutePath(), filter, true));
            }
        }

        return ret;
    }
}

Related

  1. getFiles(File dir, FilenameFilter filter)
  2. getFiles(File folder, FilenameFilter filter)
  3. getFiles(File root, boolean recurse, FileFilter fileFilter)
  4. getFiles(final File directory, final FilenameFilter filter)
  5. getFiles(String sourceDirPath, FilenameFilter filter, Comparator comparator)
  6. getFiles(String sourceName, FilenameFilter filter)
  7. getFilesOnlyFilter()
  8. getFilesRealWorking(File aStartingDir, FilenameFilter filter)