Java Folder Read getFilesInFolderByRegex(File folder, final String regex)

Here you can find the source of getFilesInFolderByRegex(File folder, final String regex)

Description

get Files In Folder By Regex

License

Apache License

Exception

Parameter Description
FileNotFoundException an exception

Return

a list of files in the given folder that matches the regex. It may be empty, but will never return a null

Declaration

private static File[] getFilesInFolderByRegex(File folder, final String regex) throws FileNotFoundException 

Method Source Code

//package com.java2s;
/*//w  w  w.  j  av a2 s. c o  m
 * Copyright 2012 LinkedIn Corp.
 *
 * Licensed 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.FileNotFoundException;

import java.io.FilenameFilter;

public class Main {
    /**
     * 
     * @return a list of files in the given folder that matches the regex. It may be empty, but will
     *         never return a null
     * @throws FileNotFoundException
     */
    private static File[] getFilesInFolderByRegex(File folder, final String regex) throws FileNotFoundException {
        // sanity check

        if (!folder.exists()) {
            throw new FileNotFoundException();

        }
        if (!folder.isDirectory()) {
            throw new IllegalStateException(
                    "execution jar is suppose to be in this folder, but the object present is not a directory: "
                            + folder);
        }

        File[] matchingFiles = folder.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                if (name.matches(regex))
                    return true;
                else
                    return false;
            }
        });

        if (matchingFiles == null) {
            throw new IllegalStateException(
                    "the File[] matchingFiles variable is null.  This means an IOException occured while doing listFiles.  Please check disk availability and retry again");
        }

        return matchingFiles;
    }
}

Related

  1. getFileSimpleName(final File f)
  2. getFilesInClassPath(String path, String endsWith)
  3. getFilesIncludeSubdirectories(final File directory)
  4. getFilesInFolder(File dir)
  5. getFilesInFolder(final String pathToFolder, final String suffix)
  6. getFilesMatchingRegexp(final File directory, final String regexp)
  7. getFilesModDate(String path)
  8. getFilesOf(File dir)
  9. getFilesOfDirectory(File directory)