Java File Find findByExtension(final File directory, final String extension)

Here you can find the source of findByExtension(final File directory, final String extension)

Description

Find the files with an extension inside a directory.

License

Open Source License

Parameter

Parameter Description
directory the directory to search
extension the extension to search for

Return

the files inside the directory with the extension

Declaration

public static List<File> findByExtension(final File directory, final String extension) 

Method Source Code


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

import java.io.File;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Main {
    /**//  w w w.  j  a v  a 2s  .  c  o m
     * Find the files with an extension inside a directory.
     *
     * @param directory
     *            the directory to search
     * @param extension
     *            the extension to search for
     * @return the files inside the directory with the extension
     */
    public static List<File> findByExtension(final File directory, final String extension) {
        assert directory.isDirectory();

        File[] res = directory.listFiles(fileName -> fileName.getName().endsWith(extension));
        if (res == null) {
            return Collections.emptyList();
        }
        return Arrays.asList(res);
    }
}

Related

  1. find(String name, File dir)
  2. find(String path, String matcher)
  3. find(String path, String suffix)
  4. find2(File baseDir, FileFilter filter, List files, boolean includeHiddenFiles)
  5. findByExt(File base, String ext)
  6. findByFileName(List files, String fileName)
  7. findExe(String exeName, String... path)
  8. findExecutable(File baseLocation)
  9. findExecutable(String executableName)