Java Folder Read Recursive getFiles(File directory, boolean recursive)

Here you can find the source of getFiles(File directory, boolean recursive)

Description

Get all files in a directory

License

Apache License

Parameter

Parameter Description
directory The directory to get the files from
recursive Whether to include subdirectories or not

Return

A list of files in the given directory

Declaration

public static List<File> getFiles(File directory, boolean recursive) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

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

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

public class Main {
    /**//from   w w  w.j a va  2  s  .com
     * Get all files in a directory
     * @param directory   The directory to get the files from
     * @param recursive   Whether to include subdirectories or not
     * @return A list of files in the given directory
     */
    public static List<File> getFiles(File directory, boolean recursive) {
        List<File> fileList = new ArrayList<File>();

        if (!directory.isDirectory()) { //File is not a directory, return file itself (if has correct extension)
            fileList.add(directory);
            return fileList;
        }

        //Get all files in this directory
        File[] files = directory.listFiles();

        //Recursively add the files
        for (File f : files) {
            if (f.isDirectory()) {
                if (recursive)
                    fileList.addAll(getFiles(f, true));
            } else
                fileList.add(f);
        }

        return fileList;
    }

    /**
     * Get all files in a directory
     * @param directory   The directory to get the files from
     * @param extension   The extension of the files to get, without the dot so e.g. "gpml"
     * @param recursive   Whether to include subdirectories or not
     * @return A list of files with given extension present in the given directory
     */
    public static List<File> getFiles(File directory, final String extension, boolean recursive) {
        List<File> fileList = new ArrayList<File>();

        if (!directory.isDirectory()) { //File is not a directory, return file itself (if has correct extension)
            if (directory.getName().endsWith("." + extension))
                fileList.add(directory);
            return fileList;
        }

        //Get all files in this directory
        File[] files = directory.listFiles(new FileFilter() {
            public boolean accept(File f) {
                return (f.isDirectory() || f.getName().endsWith("." + extension)) ? true : false;
            }
        });

        //Recursively add the files
        for (File f : files) {
            if (f.isDirectory()) {
                if (recursive)
                    fileList.addAll(getFiles(f, extension, true));
            } else
                fileList.add(f);
        }

        return fileList;
    }
}

Related

  1. getFiles(File dir, String excludeFiles, boolean includeFiles, boolean includeDirs, boolean recursive, List filelist)
  2. getFiles(File directory, boolean recursive)
  3. getFilesAsArrayListRecursive(String path)
  4. getFilesAsArrayListRecursiveHelper(File f, ArrayList filelist)
  5. getFilesByNameRecursive(Collection filesList, File path, String name)