Java Path File Name nio findFile(String basePath, final String fileName)

Here you can find the source of findFile(String basePath, final String fileName)

Description

TODO dir cache

License

Open Source License

Declaration

public static File findFile(String basePath, final String fileName) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.IOException;

import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

public class Main {
    /**//from w  w w . jav  a  2s .co  m
     * TODO dir cache
     */
    public static File findFile(String basePath, final String fileName) throws IOException {

        Path startPath = Paths.get(basePath);
        final List<File> fileResult = new ArrayList<>();

        // search directories
        Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                if (file.toFile().getName().equalsIgnoreCase(fileName)) {
                    fileResult.add(file.toFile());
                    return FileVisitResult.TERMINATE;
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException e) {
                return FileVisitResult.CONTINUE;
            }
        });

        // checks result
        if (fileResult.isEmpty()) {
            return null;
        }
        return fileResult.get(0);

    }
}

Related

  1. fileName(final Path path)
  2. fileName(final Path thePath)
  3. filterJarContainingClass(Collection jarsPaths, String className)
  4. findExecutablePaths(String executableName)
  5. findFile(Path directoryPath, String fileName)
  6. findNextFileName(Path folder, String name)
  7. findRootPathForResource(String resourceName, ClassLoader classLoader)
  8. findUpward(String filename, Path startingPath)
  9. formatPathName(Path path)