Java Path Resolve nio resolve(Path workingDir, List paths, String file)

Here you can find the source of resolve(Path workingDir, List paths, String file)

Description

resolve

License

Open Source License

Parameter

Parameter Description
workingDir the directory of the file currently being processed
paths the search path to use; this should contain the paths to search in top-bottom order of priority
file the relative file path

Return

Optional.of(the final path) , or Optional.empty() if the file could not be found

Declaration

public static Optional<Path> resolve(Path workingDir, List<Path> paths, String file) 

Method Source Code


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

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;

public class Main {
    /**//from w w w  .j av a 2 s  . co  m
     * @param workingDir
     *        the directory of the file currently being processed
     * @param paths
     *        the search path to use; this should contain the paths to search
     *        in top-bottom order of priority
     * @param file
     *        the relative file path
     * @return
     *         {@code Optional.of(the final path)}, or {@code Optional.empty()}
     *         if the file could not be found
     */
    public static Optional<Path> resolve(Path workingDir, List<Path> paths, String file) {
        paths.add(workingDir);
        Optional<Path> resolved = Optional.empty();
        for (Path path : paths) {
            // non-directory file: get parent
            if (Files.exists(path) && !Files.isDirectory(path))
                path = path.getParent();
            Path possibility = path.resolve(file).normalize();
            if (Files.exists(possibility) && !Files.isDirectory(possibility)) {
                resolved = Optional.of(possibility);
                break;
            }
        }
        paths.remove(paths.size() - 1);
        return resolved;
    }
}

Related

  1. resolve(Path base, String one, String... more)
  2. resolve(Path directory, String... parts)
  3. resolve(Path file, Path srcDir, Path destDir)
  4. resolve(Path path1, Path path2)
  5. resolve(Path target, Collection paths)
  6. resolveBagUri(final Path baseDir, final URI bagUri)
  7. resolveForSymbolic(final Path path)
  8. resolveMaxStep(String rootPath)
  9. resolvePath(final Path baseDirPath, final Path userPath)