Java Directory to File List getAllFiles(String path)

Here you can find the source of getAllFiles(String path)

Description

get all files in a folder

License

Apache License

Parameter

Parameter Description
path String folder path

Return

List

Declaration

public static List<File> getAllFiles(String path) 

Method Source Code


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

import java.io.*;

import java.util.*;

public class Main {
    /**//from   w w  w.  j  a  v a  2 s  .  co  m
     * get all files in a folder
     *
     * @param path String folder path
     * @return List<File>
     */
    public static List<File> getAllFiles(String path) {
        List<File> fileList = new ArrayList<File>();
        File file = new File(path);
        if (!file.exists()) {
            return fileList;
        }
        if (!file.isDirectory()) {
            return fileList;
        }
        String[] tempList = file.list();
        File tempFile;
        for (String fileName : tempList) {
            if (path.endsWith(File.separator)) {
                tempFile = new File(path + fileName);
            } else {
                tempFile = new File(path + File.separator + fileName);
            }
            if (tempFile.isFile()) {
                fileList.add(tempFile);
            }
            if (tempFile.isDirectory()) {
                List<File> allFiles = getAllFiles(tempFile.getAbsolutePath());
                fileList.addAll(allFiles);
            }
        }
        return fileList;
    }

    /**
     * get all files with specified suffix in a folder
     *
     * @param path   String folder path
     * @param suffix String the specified suffix
     * @return List<File>
     */
    public static List<File> getAllFiles(String path, String suffix) {
        List<File> fileList = new ArrayList<File>();
        File file = new File(path);
        if (!file.exists()) {
            return fileList;
        }
        if (!file.isDirectory()) {
            return fileList;
        }
        String[] tempList = file.list();
        File tempFile;
        for (String fileName : tempList) {
            if (path.endsWith(File.separator)) {
                tempFile = new File(path + fileName);
            } else {
                tempFile = new File(path + File.separator + fileName);
            }
            if (tempFile.isFile()) {
                if (suffix == null || "".equals(suffix))
                    fileList.add(tempFile);
                else {
                    String filePath = tempFile.getAbsolutePath();
                    if (!suffix.equals("")) {
                        int beginIndex = filePath.lastIndexOf("."); // the last '.' index before suffix
                        String tempSuffix;

                        if (beginIndex != -1) {
                            tempSuffix = filePath.substring(beginIndex + 1, filePath.length());
                            if (tempSuffix.equals(suffix)) {
                                fileList.add(tempFile);
                            }
                        }
                    }
                }
            }
            if (tempFile.isDirectory()) {
                List<File> allFiles = getAllFiles(tempFile.getAbsolutePath(), suffix);
                fileList.addAll(allFiles);
            }
        }
        return fileList;
    }
}

Related

  1. getAllFiles(String dir, String filePattern)
  2. getAllFiles(String directory)
  3. getAllFiles(String directory)
  4. getAllFiles(String dirPath)
  5. getAllFiles(String folderPath)
  6. getAllFiles(String path, boolean isDepth)
  7. getAllFiles(String sDirectoryPath)
  8. getAllFiles(String zipName, String fileNameRegEx)
  9. getAllFiles(String[] args, boolean recurse)