Java Directory to File List getAllFiles(String dirPath)

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

Description

Get list of files path under the directory recursively

License

Open Source License

Parameter

Parameter Description
dirPath a parameter

Declaration

public static List<String> getAllFiles(String dirPath) 

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 {
    /**//  ww  w  .  j  a  v  a2 s. co  m
     * Get  list of files path under the directory  recursively 
     * @param dirPath
     * @return
     */
    public static List<String> getAllFiles(String dirPath) {

        List<String> files = new ArrayList<>();
        File folder = new File(dirPath);
        File[] listOfFiles = folder.listFiles();

        for (int i = 0; i < listOfFiles.length; i++) {
            File file = listOfFiles[i];
            String filePath = file.getPath();
            if (file.isFile()) {

                if (!file.isHidden() && !file.getName().startsWith("_"))
                    files.add(filePath);
            } else if (file.isDirectory()) {

                files.addAll(getAllFiles(filePath));
            }
        }

        return files;
    }
}

Related

  1. getAllFiles(String dir)
  2. getAllFiles(String dir, String extension)
  3. getAllFiles(String dir, String filePattern)
  4. getAllFiles(String directory)
  5. getAllFiles(String directory)
  6. getAllFiles(String folderPath)
  7. getAllFiles(String path)
  8. getAllFiles(String path, boolean isDepth)
  9. getAllFiles(String sDirectoryPath)