Java Directory to File List getAllFilesInFolderAndSubFolders(String folder)

Here you can find the source of getAllFilesInFolderAndSubFolders(String folder)

Description

get All Files In Folder And Sub Folders

License

Open Source License

Declaration

public static String[] getAllFilesInFolderAndSubFolders(String folder) 

Method Source Code

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

import java.io.File;

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

public class Main {
    public static String[] getAllFilesInFolderAndSubFolders(String folder) {
        List<File> filesToCheck = new ArrayList<File>();
        File file = load(folder);
        if (file != null) {
            if (file.exists()) {
                if (file.isDirectory()) {
                    for (File f : file.listFiles()) {
                        filesToCheck.add(f);
                    }//from w  w  w.ja v  a 2  s.  c o m
                }
            }
            boolean foldersInList = true;
            while (foldersInList) {
                List<File> remove = new ArrayList<File>();
                List<File> toCheck = new ArrayList<File>();
                for (File f : filesToCheck) {
                    if (f.isDirectory()) {
                        remove.add(f);
                        for (File fi : f.listFiles()) {
                            toCheck.add(fi);
                        }
                    }
                }
                for (File f : toCheck) {
                    filesToCheck.add(f);
                }
                for (File f : remove) {
                    filesToCheck.remove(f);
                }
                if (remove.size() == 0) {
                    foldersInList = false;
                }
            }
            String[] files = new String[filesToCheck.size()];
            for (int i = 0; i < files.length; i++) {
                files[i] = filesToCheck.get(i).getPath();
            }
            return files;
        }
        return new String[] {};
    }

    public static File load(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            return file;
        }
        return null;
    }

    public static boolean isDirectory(String filePath) {
        File file = load(filePath);
        if (file != null) {
            return file.isDirectory();
        }
        return false;
    }
}

Related

  1. getAllFilesInDirectory(String dirName)
  2. getAllFilesInDirectory(String path)
  3. getAllFilesInDirMatchingPattern(String directory, final String regex, final boolean first_match2)
  4. getAllFilesInFolder(File rootFolder, FileFilter filter, int maxFilesRequired)
  5. getAllFilesInFolder(String folder)
  6. getAllFilesInHierarchy(final String basePath, final FilenameFilter filter)
  7. getAllFilesInSubFolder(String base, String ending)
  8. getAllFilesInternal(final File aPath, final FilenameFilter filter, final List fileList)
  9. getAllFilesLeastRecentFirst(File directory)