Java Folder Read getFilesAndFilesSubDirectories(String directoryName, String regexPattern)

Here you can find the source of getFilesAndFilesSubDirectories(String directoryName, String regexPattern)

Description

get Files And Files Sub Directories

License

LGPL

Parameter

Parameter Description
directoryName a parameter
regexPattern a parameter

Declaration

private static List<File> getFilesAndFilesSubDirectories(String directoryName, String regexPattern) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static final String allFilterRegex = ".*";

    /**/*from   w  w w  . j  ava  2  s  .c o  m*/
     *
     * @param directoryName
     * @param regexPattern
     * @return
     */
    private static List<File> getFilesAndFilesSubDirectories(String directoryName, String regexPattern) {

        File directory = new File(directoryName);

        //get all the files from a directory
        File[] fList = directory.listFiles();
        List<File> fileList = new ArrayList<File>();
        if (fList == null) {
            return fileList;
        }
        for (File file : fList) {
            if (!file.getName().matches(regexPattern)) {
                continue;
            }
            fileList.add(file);
            if (file.isDirectory()) {
                getFilesAndFilesSubDirectories(file.getAbsolutePath(), regexPattern);
            }
        }
        return fileList;
    }

    /**
     * list folders and file by String pattern
     * example directoryName= /home/MyUser/Dekstop/myfiles/
     * pattern=*
     * @param directoryName to be listed
     */
    public static List<File> listFiles(String directoryName) {
        return listFilesByRegex(directoryName, allFilterRegex);
    }

    /**
     *
     * @param directoryName
     * @param pattern
     * @return
     */
    public static List<File> listFiles(String directoryName, String pattern) {
        String regexPattern = pattern == null ? allFilterRegex : pattern.replace(".", "\\.").replace("*", ".*");
        return listFilesByRegex(directoryName, regexPattern);
    }

    /**
     *  list  files by regex Pattern
     * example directoryName= /home/MyUser/Dekstop/myfiles/
     * example pattern =/.*.json/
     *
     * @param directoryName
     * @param regexPattern
     * @return
     */
    public static List<File> listFilesByRegex(String directoryName, String regexPattern) {

        File directory = new File(directoryName);

        //get all the files from a directory
        File[] fList = directory.listFiles();
        List<File> fileList = new ArrayList<File>();
        if (fList == null) {
            return fileList;
        }

        for (File file : fList) {
            if (file.isFile() && file.getName().matches(regexPattern)) {
                fileList.add(file);
            }
        }
        return fileList;
    }
}

Related

  1. getFiles(String path, String endian)
  2. getFiles(String paths)
  3. getFiles(String realpath, List files, String[] fileType)
  4. getFiles(String rootDirectory)
  5. getFiles(String thePaths[])
  6. getFilesArray(File baseStorePath, final String fileNameSearchPattern)
  7. getFilesAsArrayList(String path)
  8. getFilesByDate(String directory, final boolean earliestFirst)
  9. getFilesByName(File path, String name)