Get Files in Directory Recursively

Get all files in a specified directory recursively.

Javadoc available at https://www.javatapas.com/docs/javatapas/io/GetFilesInDirectoryRecursively.html


public static void getFilesInDirectoryRecursively(String directoryName, List<File> files) {getFilesInDirectoryRecursively(directoryName, files, null);}

public static void getFilesInDirectoryRecursively(String directoryName, List<File> files, String extension) {

  File directory = new File(directoryName);

  // Get all files from a directory.
  File[] fList = directory.listFiles();
  if(fList != null){
      for (File file : fList) {
          if (file.isFile()) {
            if (extension == null){files.add(file);}
            else if (file.getName().endsWith(extension)){files.add(file);}
            else {} // skip file
          } else if (file.isDirectory()) {
              getFilesInDirectoryRecursively(file.getAbsolutePath(), files);
          }
      }
  }

}