Java Directory to File List getAllFilesInHierarchy(final String basePath, final FilenameFilter filter)

Here you can find the source of getAllFilesInHierarchy(final String basePath, final FilenameFilter filter)

Description

get All Files In Hierarchy

License

Open Source License

Declaration

public static File[] getAllFilesInHierarchy(final String basePath, final FilenameFilter filter)
            throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 005, 2007, 2008 committers of openArchitectureWare and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from   w ww  . j ava  2s  . c o  m*/
 *     committers of openArchitectureWare - initial API and implementation
 *******************************************************************************/

import java.io.File;

import java.io.FilenameFilter;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static File[] getAllFilesInHierarchy(final String basePath, final FilenameFilter filter)
            throws IOException {
        return getAllFiles(new File(basePath), filter);
    }

    public static File[] getAllFiles(final File file, final FilenameFilter filter) throws IOException {
        if (!file.isDirectory())
            throw new IOException(file.getPath() + " is not a directory!");
        final List<File> returnList = new ArrayList<File>();
        getAllFilesInternal(file, filter, returnList);
        return returnList.toArray(new File[returnList.size()]);
    }

    private static void getAllFilesInternal(final File aPath, final FilenameFilter filter,
            final List<File> fileList) {
        final File[] allFiles = aPath.listFiles(filter);
        for (int i = 0; i < allFiles.length; i++) {
            if (allFiles[i].isDirectory())
                getAllFilesInternal(allFiles[i], filter, fileList);
            else
                fileList.add(allFiles[i]);
        }
    }
}

Related

  1. getAllFilesInDirectory(String path)
  2. getAllFilesInDirMatchingPattern(String directory, final String regex, final boolean first_match2)
  3. getAllFilesInFolder(File rootFolder, FileFilter filter, int maxFilesRequired)
  4. getAllFilesInFolder(String folder)
  5. getAllFilesInFolderAndSubFolders(String folder)
  6. getAllFilesInSubFolder(String base, String ending)
  7. getAllFilesInternal(final File aPath, final FilenameFilter filter, final List fileList)
  8. getAllFilesLeastRecentFirst(File directory)
  9. getAllFilesMatching(File srcDir, final String regex)