Java Folder Read getFilesInFolder(File dir)

Here you can find the source of getFilesInFolder(File dir)

Description

Returns all the Files in the specified directory and all sub-directories.

License

LGPL

Parameter

Parameter Description
directory The directory to check.

Return

All the files in the folder and sub folders

Declaration

public static File[] getFilesInFolder(File dir) 

Method Source Code

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

import java.io.File;

import java.util.ArrayList;

public class Main {
    /**/*from   w w w . j a  va 2  s.c  o m*/
     * Returns all the Files in the specified directory and all sub-directories.
     *
     * <p>
     * For instance, If you have a folder, /Files/Documents/Maps, and call this method for Hello. It will return all the files in Documents and all the files in Maps!
     *
     * @param directory
     *            The directory to check.
     * @return All the files in the folder and sub folders
     */
    public static File[] getFilesInFolder(File dir) {
        return files(dir).toArray(new File[0]);
    }

    private static ArrayList<File> files(File dir) {
        ArrayList<File> files = new ArrayList<File>();

        if (!dir.isDirectory())
            throw new IllegalArgumentException("dir Isn't a Directory! " + dir);

        for (int i = 0; i < dir.listFiles().length; i++) {
            if (dir.listFiles()[i].isDirectory()) {
                files.addAll(files(dir.listFiles()[i]));
            }
            files.add(dir.listFiles()[i]);
        }

        return files;
    }
}

Related

  1. getFilesFromClassPath(String classpath)
  2. getFilesFromDiskWorker(File folderToLoad, final String searchPattern, final boolean justFolders)
  3. getFileSimpleName(final File f)
  4. getFilesInClassPath(String path, String endsWith)
  5. getFilesIncludeSubdirectories(final File directory)
  6. getFilesInFolder(final String pathToFolder, final String suffix)
  7. getFilesInFolderByRegex(File folder, final String regex)
  8. getFilesMatchingRegexp(final File directory, final String regexp)
  9. getFilesModDate(String path)