Java Directory to File List getAllFiles(String path, boolean isDepth)

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

Description

get all files in a folder (Only file)

License

Open Source License

Parameter

Parameter Description
path String folder path
isDepth true is need to scan all subdirectories

Return

List of File that content all file in the folder

Declaration

public static List<File> getAllFiles(String path, boolean isDepth) 

Method Source Code


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

import java.io.*;

import java.util.*;
import java.util.stream.Collectors;

public class Main {
    /**/* ww w.j  a  va2 s. c o m*/
     * get all files in a folder (Only file)
     *
     * @param path
     *       String folder path
     * @param isDepth
     *       true is need to scan all subdirectories
     * @return List of File that content all file in the folder
     */
    public static List<File> getAllFiles(String path, boolean isDepth) {
        List<File> fileList = new ArrayList<>();
        File file = new File(path);
        File[] tempList = file.listFiles();

        if (tempList == null || !file.exists() || !file.isDirectory()) {
            return fileList;
        }

        for (File tempFile : tempList) {
            if (isDepth) {
                if (tempFile.isFile()) {
                    fileList.add(tempFile);
                }
                if (tempFile.isDirectory()) {
                    List<File> allFiles = getAllFiles(tempFile.getAbsolutePath(), true);
                    fileList.addAll(allFiles);
                }
            } else {
                if (!tempFile.isDirectory()) {
                    fileList.add(tempFile);
                }
            }
        }
        return fileList;
    }

    /**
     * add all files that matched with extension <br>
     * {@link #getAllFiles(String, String, boolean)}with boolean is {@code true}
     *
     * @param path
     *       folder path
     * @param extension
     *       file extension
     * @return all file matching extension
     */
    public static List<File> getAllFiles(String path, String extension) {
        return getAllFiles(path, extension, true);
    }

    /**
     * get all file from the folder
     *
     * @param path
     *       folder path
     * @param extension
     *       filter file extension
     * @param isDepth
     *       recursive reading sub folder
     * @return all file matching extension
     */
    public static List<File> getAllFiles(String path, String extension, boolean isDepth) {
        return getAllFiles(path, isDepth).stream().filter(file -> extension.equals(getExtension(file.getName())))
                .collect(Collectors.toList());
    }

    /**
     * check is path a directory
     *
     * @param path
     *       absolute folder path
     * @return true if path is directory path
     */
    public static boolean isDirectory(String path) {
        return new File(path).isDirectory();
    }

    /**
     * get file extension/suffix (without dot) <br>
     * Example: html, txt, pdf, etc.
     *
     * @param fileName
     *       file name
     * @return extension/suffix of file
     */
    public static String getExtension(String fileName) {
        String[] name = fileName.split("\\.");
        if (name.length < 2)
            return "";
        return name[name.length - 1];
    }
}

Related

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