Java Path Resolve nio resolve(Path path1, Path path2)

Here you can find the source of resolve(Path path1, Path path2)

Description

Resolves paths, even if their file system are different.

License

CDDL license

Parameter

Parameter Description
path1 the given path
path2 the path to resolve against the given path

Return

the resolved path

Declaration

public static Path resolve(Path path1, Path path2) 

Method Source Code


//package com.java2s;
/*//  www. j av  a 2  s .  c  o m
 *         COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Notice
 *
 * The contents of this file are subject to the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL)
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.opensource.org/licenses/cddl1.txt
 *
 * The Original Code is SoftSmithy Utility Library. The Initial Developer of the
 * Original Code is Florian Brunner (Sourceforge.net user: puce). All Rights Reserved.
 *
 * Contributor(s): .
 */

import java.nio.file.Path;

public class Main {
    /**
     * Resolves paths, even if their file system are different.
     * 
     * If the file system is the same, {@link Path#resolve(java.nio.file.Path)} is used.
     * 
     * If {@code path2} is {@link Path#isAbsolute() absolute}, {@code path2} is returned.
     * 
     * Else the resolved path gets calculated.
     * 
     * @param path1 the given path
     * @param path2 the path to resolve against the given path
     * @return the resolved path
     */
    public static Path resolve(Path path1, Path path2) {
        if (path1.getFileSystem().equals(path2.getFileSystem())) {
            return path1.resolve(path2);
        } else {
            if (path2.isAbsolute()) {
                return path2;
            } else {
                Path resolvedPath = path1;
                for (Path pathPart : path2) {
                    resolvedPath = resolvedPath.resolve(pathPart.toString());
                }
                return resolvedPath;
            }
        }
    }
}

Related

  1. getResolvableFilePath(String path)
  2. resolve(final Optional parent, final String child)
  3. resolve(Path base, String one, String... more)
  4. resolve(Path directory, String... parts)
  5. resolve(Path file, Path srcDir, Path destDir)
  6. resolve(Path target, Collection paths)
  7. resolve(Path workingDir, List paths, String file)
  8. resolveBagUri(final Path baseDir, final URI bagUri)
  9. resolveForSymbolic(final Path path)