Java Path Relative nio toFileWithAbsolutePath(String relativePath)

Here you can find the source of toFileWithAbsolutePath(String relativePath)

Description

Upstream, based on branch 'master' of git@github.com:adaussy/EMFCompareGitPGM.git Returns, from a relative path, the corresponding file with an absolute path.

License

Open Source License

Parameter

Parameter Description
relativePath the relative path for which we want the corresponding file.

Return

the corresponding file with an absolute path.

Declaration

public static File toFileWithAbsolutePath(String relativePath) 

Method Source Code


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

import java.io.File;

import java.nio.file.FileSystems;
import java.nio.file.Path;

public class Main {
    /**//from  w  w w  . j  ava 2  s  .c  o  m
     * Upstream, based on branch 'master' of git@github.com:adaussy/EMFCompareGitPGM.git Returns, from a
     * relative path, the corresponding file with an absolute path. This absolute path is computed against
     * 'user.dir' system property.
     * 
     * @param relativePath
     *            the relative path for which we want the corresponding file.
     * @return the corresponding file with an absolute path.
     */
    public static File toFileWithAbsolutePath(String relativePath) {
        return toFileWithAbsolutePath(System.getProperty("user.dir"), relativePath); //$NON-NLS-1$
    }

    /**
     * Returns, from a relative path, the corresponding file with an absolute path. This absolute path is
     * computed against the given base path.
     * 
     * @param relativePath
     *            the relative path for which we want the corresponding file.
     * @return the corresponding file with an absolute path.
     */
    public static File toFileWithAbsolutePath(String basePath, String relativePath) {
        File file = new File(relativePath);
        if (!file.isAbsolute()) {
            Path base = FileSystems.getDefault().getPath(basePath);
            Path resolvedPath = base.resolve(file.toPath());
            Path absolutePath = resolvedPath.normalize();
            file = absolutePath.toFile();
        }
        return file;
    }
}

Related

  1. relativizePath(String basePath, File toRelativize)
  2. resolvePathIfRelativeTo(File referenceFile, String partialPath)
  3. resolveRelative(Path p, String other)
  4. resolveRelativePath(String first, String... more)
  5. resolveRelativeRemotePath(Path root, Path file)
  6. tryMakeRelative(String rootDir, String path)