Here you can find the source of findFilesWithExtension(Path directory, String targetExtension)
Parameter | Description |
---|---|
directory | Directory to traverse |
targetExtension | Target extension to look for |
Parameter | Description |
---|---|
IOException | an exception |
public static List<Path> findFilesWithExtension(Path directory, String targetExtension) throws IOException
//package com.java2s; //License from project: Open Source 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.Iterator; import java.util.List; public class Main { /**//from ww w. j a va 2s. co m * Traverses a directory to find the list of files with the target extension * * @param directory Directory to traverse * @param targetExtension Target extension to look for * @return List of files in the directory with the target extension * @throws IOException */ public static List<Path> findFilesWithExtension(Path directory, String targetExtension) throws IOException { List<Path> files = new ArrayList<>(); try (DirectoryStream<Path> ds = Files.newDirectoryStream(directory)) { for (Iterator<Path> it = ds.iterator(); it.hasNext();) { // Gets a file path Path file = (Path) it.next(); String extension = getExtension(file).toLowerCase(); // If the extension is .h, adds it to the header files list if (extension.equals(targetExtension.toLowerCase())) { files.add(file); } else { continue; } } } return files; } /** * Gets the extension of a file * * @param file Path to the file * @return Extension * @throws Exception File not have extension */ public static String getExtension(Path file) { String fullFilename = file.getFileName().toString(); String[] tokens = fullFilename.split("\\."); // if the file does not have an extension if (tokens.length <= 1) { return fullFilename; } return tokens[tokens.length - 1]; } }