Here you can find the source of getAllFiles(String path, boolean isDepth)
Parameter | Description |
---|---|
path | String folder path |
isDepth | true is need to scan all subdirectories |
public static List<File> getAllFiles(String path, boolean isDepth)
//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]; } }