Here you can find the source of getListOfFilesByExtension(Path directoryPath, Set
Parameter | Description |
---|---|
directoryPath | path to directory that will be searched for eligible files |
extensions | files with what extensions should be included |
Parameter | Description |
---|---|
IOException | an exception |
public static List<Path> getListOfFilesByExtension(Path directoryPath, Set<String> extensions) throws IOException
//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; } }