Java Folder Read getFiles(Collection folders)

Here you can find the source of getFiles(Collection folders)

Description

This method recursively gets a collection of all the files in a collection of folders/files.

License

Open Source License

Parameter

Parameter Description
folders A collection of folders to get all files from

Return

A list of all files in a folder

Declaration

public static Collection<File> getFiles(Collection<File> folders) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class Main {
    /**/*from   w  w  w.j  a  va2s.c  om*/
     * This method recursively gets a collection of all the files in a collection
     * of folders/files.
     * 
     * @param folders
     *          A collection of folders to get all files from
     * @return A list of all files in a folder
     */
    public static Collection<File> getFiles(Collection<File> folders) {
        List<File> fileList = new LinkedList<File>();
        for (File folder : folders) {
            if (folder.isDirectory()) {
                for (File f : folder.listFiles()) {
                    if (f.isDirectory())
                        fileList.addAll(getFiles(f));
                    else
                        fileList.add(f);
                }
            } else {
                fileList.add(folder);
            }
        }
        return fileList;
    }

    /**
     * This method recursively gets a collection of all the files in a single
     * folder
     * 
     * @param folder
     *          A folder to get all files from
     * @return A list of all files in a folder (null if input is not a folder)
     */
    public static Collection<File> getFiles(File folder) {
        if (!folder.isDirectory())
            return null;
        List<File> folderList = new ArrayList<File>(1);
        folderList.add(folder);
        return getFiles(folderList);
    }
}

Related

  1. getFiles(File apkFile, Pattern regex)
  2. getFiles(File archive)
  3. getFiles(File dir)
  4. getFiles(File dir)