Java Folder Read Recursive getFilesForFolder(File folder, boolean recursive)

Here you can find the source of getFilesForFolder(File folder, boolean recursive)

Description

Returns a sorted list of files contained in the directory.

License

Open Source License

Parameter

Parameter Description
folder a parameter
recursive a parameter

Declaration

public static List<File> getFilesForFolder(File folder,
        boolean recursive) 

Method Source Code

//package com.java2s;

import java.io.File;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

public class Main {
    /**/*ww  w.java2s .  c o m*/
     * Returns a sorted list of files contained in the directory.
     * 
     * @param folder
     * @param recursive
     * @return
     */
    public static List<File> getFilesForFolder(File folder,
            boolean recursive) {
        List<File> fileList = new ArrayList<File>();
        listFilesForFolder(fileList, folder, recursive);
        // File implements Comparable and sorts by pathname by default
        Collections.sort(fileList);
        return fileList;

    }

    private static void listFilesForFolder(final List<File> fileList,
            final File folder, boolean recursive) {
        for (final File fileEntry : folder.listFiles()) {
            if (fileEntry.isDirectory() && recursive) {
                listFilesForFolder(fileList, fileEntry, recursive);
            } else {
                fileList.add(fileEntry);
            }
        }
    }
}

Related

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