Java Directory to File List getAllFiles(File root, FileFilter fileFilter)

Here you can find the source of getAllFiles(File root, FileFilter fileFilter)

Description

get All Files

License

Open Source License

Declaration

public static File[] getAllFiles(File root, FileFilter fileFilter) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2000, 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * /*  w w w .  j  ava2 s. c o  m*/
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

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

import java.util.ArrayList;

public class Main {
    public static File[] getAllFiles(File root, FileFilter fileFilter) {
        ArrayList<File> files = new ArrayList<File>();
        if (root.isDirectory()) {
            collectAllFiles(root, files, fileFilter);
            File[] result = new File[files.size()];
            files.toArray(result);
            return result;
        }
        return null;
    }

    private static void collectAllFiles(File root, ArrayList<File> collector, FileFilter fileFilter) {
        File[] files = root.listFiles(fileFilter);
        for (int i = 0; i < files.length; i++) {
            final File currentFile = files[i];
            if (currentFile.isDirectory()) {
                collectAllFiles(currentFile, collector, fileFilter);
            } else {
                collector.add(currentFile);
            }
        }
    }
}

Related

  1. getAllFiles(File directory, String rootPath)
  2. getAllFiles(File inFileOrDir, boolean recurseDir)
  3. getAllFiles(File input)
  4. getAllFiles(File outputDir, List files)
  5. getAllFiles(File path, List fileList, boolean recursive)
  6. getAllFiles(File rootDirectory, FileFilter fileFilter, boolean includeSubdirectories)
  7. getAllFiles(File sourceDirectory, List fileList)
  8. getAllFiles(final String path, final String suffix, boolean recurse)
  9. getAllFiles(String dir)