Java Path File Extention nio findFilesWithExtension(Path directory, String targetExtension)

Here you can find the source of findFilesWithExtension(Path directory, String targetExtension)

Description

Traverses a directory to find the list of files with the target extension

License

Open Source License

Parameter

Parameter Description
directory Directory to traverse
targetExtension Target extension to look for

Exception

Parameter Description
IOException an exception

Return

List of files in the directory with the target extension

Declaration

public static List<Path> findFilesWithExtension(Path directory, String targetExtension) throws IOException 

Method Source Code


//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];
    }
}

Related

  1. asPaths(final Iterable files)
  2. changeExtension(Path source, String extension)
  3. changeFileExtension(Path fileName, String extension)
  4. fileExtension(final Path thePath)
  5. fileRenamer(final String fromPath, final String toPath, @Nullable final String toExtension, final boolean junkPaths)
  6. getAllPaths(Path root, final String... extensions)
  7. getExtension(final Path path)
  8. getExtension(Path file)
  9. getExtension(Path path)