Here you can find the source of getFileNames(List
Parameter | Description |
---|---|
ext | The file extension to look for, e.g. '.g4'. |
recursive | If the folder should be recursively traversed. |
public static List<String> getFileNames(List<String> fileNames, Path dir, String ext, boolean recursive)
//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.List; public class Main { /**//from ww w .j av a 2s .c om * Gets all files in target directory, with the given file extension. Returns an empty array list if nothing is * found or if an {@link IOException} occurs. * * @param ext The file extension to look for, e.g. '.g4'. * @param recursive If the folder should be recursively traversed. * @return List of full file names. */ public static List<String> getFileNames(List<String> fileNames, Path dir, String ext, boolean recursive) { try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) { // Iterate over paths stream.forEach(p -> { if (p.toFile().isDirectory() && recursive) { // Directory getFileNames(fileNames, p, ext, true); } else { // File String fileName = p.toAbsolutePath().toString(); if (fileName.endsWith(ext)) fileNames.add(fileName); } }); } catch (IOException e) { return new ArrayList<>(); } return fileNames; } }