Java Resource Load getFilesRecursevely(final String sourceFolder, final String classRegEx)

Here you can find the source of getFilesRecursevely(final String sourceFolder, final String classRegEx)

Description

get Files Recursevely

License

Open Source License

Declaration

public static List<File> getFilesRecursevely(final String sourceFolder,
            final String classRegEx) 

Method Source Code

//package com.java2s;
/*//from   w w w  .  ja  v a2  s  .c o  m
 * Copyright (C) 2008 J?lio Vilmar Gesser.
 * 
 * This file is part of Java 1.5 parser and Abstract Syntax Tree.
 *
 * Java 1.5 parser and Abstract Syntax Tree 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 3 of the License, or
 * (at your option) any later version.
 *
 * Java 1.5 parser and Abstract Syntax Tree 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.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Java 1.5 parser and Abstract Syntax Tree.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.io.FilenameFilter;

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

public class Main {
    public static List<File> getFilesRecursevely(String sourceFolder) {
        return getFilesRecursevely(sourceFolder, ".*");
    }

    public static List<File> getFilesRecursevely(final String sourceFolder,
            final String classRegEx) {
        return getFilesRecursevely(sourceFolder, classRegEx, true);
    }

    public static List<File> getFilesRecursevely(String sourceFolder,
            final String classRegEx, boolean searchChildrenFolders) {
        List<File> files = new ArrayList<File>();
        File folder = new File(sourceFolder);
        if (!folder.exists()) {
            throw new IllegalArgumentException("Folder " + sourceFolder
                    + " does not exist");
        }
        if (!folder.isDirectory()) {
            throw new IllegalArgumentException(sourceFolder
                    + " is not a folder");
        }
        File[] filesAndFolders = new File(sourceFolder)
                .listFiles(new FilenameFilter() {

                    public boolean accept(File dir, String name) {
                        if (name.toLowerCase().endsWith(classRegEx)) {
                            return true;
                        }
                        if (name.length() < 5) {
                            return dir.isDirectory();
                        }
                        return name.substring(0, name.length() - 5)
                                .matches(classRegEx);
                    }
                });
        for (File file : filesAndFolders) {
            if (file.isDirectory()) {
                if (searchChildrenFolders) {
                    files.addAll(getFilesRecursevely(
                            file.getAbsolutePath(), classRegEx,
                            searchChildrenFolders));
                }
                continue;
            }
            files.add(file);
        }
        return files;
    }
}

Related

  1. getFilesRecurse(File dir, final String pattern)
  2. getResource(Class c, String name)
  3. getResource(Class c, String name)
  4. getResource(Class c, String path)
  5. getResource(Class clazz, String fileName)