Java Directory to File List getAllFilesMatching(File srcDir, final String regex)

Here you can find the source of getAllFilesMatching(File srcDir, final String regex)

Description

get All Files Matching

License

Apache License

Parameter

Parameter Description
srcDir a parameter
regex a parameter

Declaration

public static List<File> getAllFilesMatching(File srcDir, final String regex) 

Method Source Code

//package com.java2s;
/* /*  w  ww.  j a  v  a2 s .c  om*/
 * Licensed to the soi-toolkit project under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The soi-toolkit project licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

import java.io.FilenameFilter;

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

public class Main {
    /**
     * 
     * @param srcDir
     * @param regex
     * @return
     */
    public static List<File> getAllFilesMatching(File srcDir, final String regex) {

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

        if (!srcDir.isDirectory()) {
            if (srcDir.getPath().matches(regex)) {
                foundFiles.add(srcDir);
                return foundFiles;
            }
            return foundFiles;
        }

        FilenameFilter filenameFiler = new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.matches(regex);
            };
        };

        foundFiles.addAll(Arrays.asList(srcDir.listFiles(filenameFiler)));

        FileFilter fileFiler = new FileFilter() {
            public boolean accept(File file) {
                return file.isDirectory();
            };
        };

        File[] directories = srcDir.listFiles(fileFiler);

        for (File file : directories) {
            foundFiles.addAll(getAllFilesMatching(file, regex));
        }

        return foundFiles;
    }
}

Related

  1. getAllFilesInFolderAndSubFolders(String folder)
  2. getAllFilesInHierarchy(final String basePath, final FilenameFilter filter)
  3. getAllFilesInSubFolder(String base, String ending)
  4. getAllFilesInternal(final File aPath, final FilenameFilter filter, final List fileList)
  5. getAllFilesLeastRecentFirst(File directory)
  6. getAllFilesMatchingThisPatternIgnoreCase(String sDirectoryPath, String sPattern)
  7. getAllFilesPresentInFolder(File srcPath)
  8. getAllFilesRecursively(File dir, List filelist)
  9. getAllFilesRecursively(File directory, Collection files)