Java Directory to File List getAllFiles(File dir)

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

Description

get All Files

License

Open Source License

Declaration

public static File[] getAllFiles(File dir) 

Method Source Code


//package com.java2s;
/*/*from   ww w. j a  va  2s.com*/
 * This program is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 */

import java.io.*;
import java.util.Vector;

public class Main {
    public static File[] getAllFiles(File dir) {
        if (!dir.isDirectory()) {
            throw new IllegalArgumentException();
        }

        Vector allFiles = new Vector();
        extractFiles(dir, allFiles);

        int size = allFiles.size();
        File[] ret = new File[size];
        for (int i = 0; i < size; i++) {
            ret[i] = (File) allFiles.elementAt(i);
        }

        return ret;
    }

    private static void extractFiles(File dir, Vector allFiles) {
        File[] files = getFiles(dir);
        File[] folders = getFolders(dir);
        int filesCount = (files == null) ? 0 : files.length;
        int foldersCount = (folders == null) ? 0 : folders.length;

        if (filesCount != 0) {
            for (int i = 0; i < filesCount; i++) {
                allFiles.addElement(files[i]);
            }
        }
        if (foldersCount != 0) {
            for (int i = 0; i < foldersCount; i++) {
                extractFiles(folders[i], allFiles);
            }
        }
    }

    public static File[] getFiles(File dir) {
        if (!dir.isDirectory()) {
            throw new IllegalArgumentException();
        }

        Vector files = new Vector();
        String[] list = dir.list();
        if ((list != null) && (list.length != 0)) {
            int len = list.length;
            for (int i = 0; i < len; i++) {
                File current = new File(dir, list[i]);
                if (current.canRead() && current.isFile()) {
                    files.addElement(current);
                }
            }

            int size = files.size();
            File[] ret = new File[size];
            for (int i = 0; i < size; i++) {
                ret[i] = (File) files.elementAt(i);
            }

            return ret;
        }

        return null;
    }

    public static File[] getFolders(File dir) {
        if (!dir.isDirectory()) {
            throw new IllegalArgumentException();
        }

        Vector files = new Vector();
        String[] list = dir.list();
        if ((list != null) && (list.length != 0)) {
            int len = list.length;
            for (int i = 0; i < len; i++) {
                File current = new File(dir, list[i]);
                if (current.canRead() && current.isDirectory()) {
                    files.addElement(current);
                }
            }

            int size = files.size();
            File[] ret = new File[size];
            for (int i = 0; i < size; i++) {
                ret[i] = (File) files.elementAt(i);
            }

            return ret;
        }

        return null;
    }
}

Related

  1. getAllFileNamesByPath(String path)
  2. getAllFileNamesInDir(String folderName)
  3. getAllFileNamesWithExtension(String directory, final String extension)
  4. getAllFilePath(String filedir)
  5. getAllFiles(File dir)
  6. getAllFiles(File dir, List fileList)
  7. getAllFiles(File dir, List fileList)
  8. getAllFiles(File dir, Pattern filter)
  9. getAllFiles(File directory)