Java Directory to File List getAllFile(File directory, boolean descendIntoSubDirectories, String endofFile, String hiddenFileSuffix)

Here you can find the source of getAllFile(File directory, boolean descendIntoSubDirectories, String endofFile, String hiddenFileSuffix)

Description

get All File

License

Open Source License

Declaration

public static ArrayList<File> getAllFile(File directory, boolean descendIntoSubDirectories, String endofFile,
            String hiddenFileSuffix) throws IOException 

Method Source Code

//package com.java2s;

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static ArrayList<File> getAllFile(File directory, boolean descendIntoSubDirectories, String endofFile,
            String hiddenFileSuffix) throws IOException {
        ArrayList<File> resultList = new ArrayList<File>();
        File[] f = directory.listFiles();
        for (File file : f) {
            if (file != null && file.getName().toLowerCase().endsWith(endofFile)
                    && !file.getName().startsWith("tn_") && !file.getName().startsWith(hiddenFileSuffix)) {
                resultList.add(file);//from ww w. j  a  v  a 2 s.  c  o  m
            }
            if (descendIntoSubDirectories && file.isDirectory()) {
                ArrayList<File> tmp = getAllFile(file, true, endofFile, hiddenFileSuffix);
                if (tmp != null) {
                    resultList.addAll(tmp);
                }
            }
        }
        if (resultList.size() > 0)
            return resultList;
        else
            return null;
    }

    public static ArrayList<File> getAllFile(File directory, boolean descendIntoSubDirectories, Pattern pattern,
            String hiddenFileSuffix) throws IOException {
        ArrayList<File> resultList = new ArrayList<File>();
        File[] f = directory.listFiles();
        Matcher matcher = null;
        for (File file : f) {

            matcher = pattern.matcher(file.getName().toLowerCase().trim());

            if (file != null && matcher.find() && !file.getName().startsWith("tn_")
                    && !file.getName().startsWith(hiddenFileSuffix)) {
                resultList.add(file);
            }
            if (descendIntoSubDirectories && file.isDirectory()) {
                ArrayList<File> tmp = getAllFile(file, true, pattern, hiddenFileSuffix);
                if (tmp != null) {
                    resultList.addAll(tmp);
                }
            }
        }
        if (resultList.size() > 0)
            return resultList;
        else
            return null;
    }
}

Related

  1. getAllFile(List list, File rootFile)
  2. getAllFile2(String Url)
  3. getAllFileFromPath(File file, List fileList, String ed)
  4. getAllFileName(String path)