Java Directory to File List getAllFilesInDirectory(String directoryPath)

Here you can find the source of getAllFilesInDirectory(String directoryPath)

Description

get All Files In Directory

License

Apache License

Declaration

public static List<String> getAllFilesInDirectory(String directoryPath) throws FileNotFoundException 

Method Source Code


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

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<String> getAllFilesInDirectory(String directoryPath) throws FileNotFoundException {
        return getAllFilesInDirectory(directoryPath, null);
    }//from  w ww .j a  v a  2 s. c o m

    public static List<String> getAllFilesInDirectory(String directoryPath, FileFilter filter)
            throws FileNotFoundException {
        List<String> filesList = new ArrayList<>();

        File directory = new File(directoryPath);

        File[] fileObjList;

        if (filter != null) {
            fileObjList = directory.listFiles(filter);
        } else {
            fileObjList = directory.listFiles();
        }
        if (fileObjList == null) {
            throw new FileNotFoundException(directoryPath + " does not exist.");
        }
        for (File file : fileObjList) {
            if (file.isFile()) {
                filesList.add(file.getAbsolutePath());
            }
        }
        return filesList;
    }
}

Related

  1. getAllFilesIn(File dir, final String type)
  2. getAllFilesInAllSubDirectories(File parentDir, FileFilter filter)
  3. getAllFilesInDir(boolean traverseSubDirs, File dir, boolean includeHidden, String prefix)
  4. getAllFilesInDirectory(File dir)
  5. getAllFilesInDirectory(File directory, List files)
  6. getAllFilesInDirectory(String dirName)
  7. getAllFilesInDirectory(String path)
  8. getAllFilesInDirMatchingPattern(String directory, final String regex, final boolean first_match2)
  9. getAllFilesInFolder(File rootFolder, FileFilter filter, int maxFilesRequired)