Java Directory to File List getAllFilesInSubFolder(String base, String ending)

Here you can find the source of getAllFilesInSubFolder(String base, String ending)

Description

Scan the base folder, and return a list of all files with the given name ending

License

Open Source License

Parameter

Parameter Description
base a parameter
ending a parameter

Declaration

public static List<File> getAllFilesInSubFolder(String base, String ending) throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/**//from   w  ww  . j  av a2s. c  o  m
 * Copyright (C) 2011,2012 Gordon Fraser, Andrea Arcuri and EvoSuite
 * contributors
 *
 * This file is part of EvoSuite.
 *
 * EvoSuite is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * EvoSuite 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 Public License for more details.
 *
 * You should have received a copy of the GNU Public License along with
 * EvoSuite. If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.util.LinkedList;
import java.util.List;

public class Main {
    /**
     * Scan the <code>base</code> folder, and return a list of all files with the given name <code>ending</code>
     * 
     * @param base
     * @param ending
     * @return
     */
    public static List<File> getAllFilesInSubFolder(String base, String ending) throws IllegalArgumentException {

        File dir = new File(base);
        if (!dir.exists() || !dir.isDirectory()) {
            throw new IllegalArgumentException("Invalid folder " + base);
        }

        List<File> files = new LinkedList<File>();
        recursiveFileSearch(files, dir, ending);

        return files;
    }

    private static void recursiveFileSearch(List<File> files, File dir, String ending) {

        for (File file : dir.listFiles()) {
            if (file.isDirectory()) {
                recursiveFileSearch(files, file, ending);
            } else {
                if (file.getName().endsWith(ending)) {
                    files.add(file);
                }
            }
        }

    }
}

Related

  1. getAllFilesInDirMatchingPattern(String directory, final String regex, final boolean first_match2)
  2. getAllFilesInFolder(File rootFolder, FileFilter filter, int maxFilesRequired)
  3. getAllFilesInFolder(String folder)
  4. getAllFilesInFolderAndSubFolders(String folder)
  5. getAllFilesInHierarchy(final String basePath, final FilenameFilter filter)
  6. getAllFilesInternal(final File aPath, final FilenameFilter filter, final List fileList)
  7. getAllFilesLeastRecentFirst(File directory)
  8. getAllFilesMatching(File srcDir, final String regex)
  9. getAllFilesMatchingThisPatternIgnoreCase(String sDirectoryPath, String sPattern)