Java Path File Extention nio getListOfFilesByExtension(Path directoryPath, Set extensions)

Here you can find the source of getListOfFilesByExtension(Path directoryPath, Set extensions)

Description

get List Of Files By Extension

License

Apache License

Parameter

Parameter Description
directoryPath path to directory that will be searched for eligible files
extensions files with what extensions should be included

Exception

Parameter Description
IOException an exception

Return

list of paths to files with extension that is included in provided set of extensions

Declaration

public static List<Path> getListOfFilesByExtension(Path directoryPath, Set<String> extensions)
        throws IOException 

Method Source Code


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

import java.io.IOException;

import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class Main {
    /**//ww  w .  j  a va  2s . c  om
     * 
     * @param directoryPath path to directory that will be searched for eligible files
     * @param extensions files with what extensions should be included
     * @return list of paths to files with extension that is included in provided set of extensions
     * @throws IOException
     */
    public static List<Path> getListOfFilesByExtension(Path directoryPath, Set<String> extensions)
            throws IOException {
        List<Path> result = new ArrayList<>();

        DirectoryStream.Filter<Path> filter = (fileEntry) -> {
            return extensions.contains(com.google.common.io.Files.getFileExtension(fileEntry.toString()));
        };

        try (DirectoryStream<Path> stream = Files.newDirectoryStream(directoryPath, filter)) {
            for (Path file : stream) {
                result.add(file);
            }
        }

        return result;
    }
}

Related

  1. getFileNameWithoutExtension(Path path)
  2. getFileNameWithoutExtension(String filePath)
  3. getFilesList(Path directory, Set extensions)
  4. getFullNameWithoutExtension(Path f)
  5. getLeafName(Path path, boolean includeExtension)
  6. getPath(String outputDir, String qualifiedFilename, String fileextension)
  7. getTransformedOutputPath(Path input, String compressExtension, String outputDir)
  8. hasExtensionIgnoreCase(Path path, String ext)
  9. insertSuffixBeforeExtension(String path, String suffix)