Java Path File Name nio findUpward(String filename, Path startingPath)

Here you can find the source of findUpward(String filename, Path startingPath)

Description

Find a file with the given base name, searching upward from the given starting directory.

License

Apache License

Parameter

Parameter Description
filename the base filename to look for
startingPath the starting directory to search upward from

Return

an Optional containing the Path of the file if found, or an empty Optional otherwise

Declaration

public static Optional<Path> findUpward(String filename, Path startingPath) 

Method Source Code


//package com.java2s;
/*/*  w  w  w .  ja v  a  2 s .c o  m*/
 * Copyright 2014 Yash Parghi
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    /**
     * Find a file with the given base name, searching upward from the given starting directory.
     *
     * @param filename the base filename to look for
     * @param startingPath the starting directory to search upward from
     *
     * @return an {@code Optional} containing the Path of the file if found, or an empty Optional
     *     otherwise
     */
    public static Optional<Path> findUpward(String filename, Path startingPath) {
        Preconditions.checkNotNull(filename);
        Preconditions.checkArgument(!filename.contains(File.separator));
        Preconditions.checkArgument(Files.isDirectory(startingPath));
        Path possibleRootDir = startingPath;
        while (possibleRootDir != null) {
            Path possiblePath = possibleRootDir.resolve(filename);
            if (Files.isRegularFile(possiblePath)) {
                return Optional.of(possiblePath);
            } else {
                possibleRootDir = possibleRootDir.getParent();
            }
        }
        return Optional.absent();
    }
}

Related

  1. findExecutablePaths(String executableName)
  2. findFile(Path directoryPath, String fileName)
  3. findFile(String basePath, final String fileName)
  4. findNextFileName(Path folder, String name)
  5. findRootPathForResource(String resourceName, ClassLoader classLoader)
  6. formatPathName(Path path)
  7. getAbsolutePath(String fileName)
  8. getAbsolutePath(String name)
  9. getAbsolutePathName(File dir)