Java Path Relative nio getRelativePath(final File file, final File baseDir)

Here you can find the source of getRelativePath(final File file, final File baseDir)

Description

Gets the relative from the specified directory to the specified file.

License

Open Source License

Parameter

Parameter Description
file The file to create a relative path for.
baseDir The base directory the path to the file should be relative to.

Return

The relative path from the base directory to the specified file.

Declaration

public static Path getRelativePath(final File file, final File baseDir) 

Method Source Code


//package com.java2s;
import java.io.File;
import java.nio.file.Path;

public class Main {
    /**/*from  w  ww .  jav a  2  s.c  om*/
     * Gets the relative from the specified directory to the specified file.
     * 
     * @param file
     *      The file to create a relative path for.
     * @param baseDir
     *      The base directory the path to the file should be relative to.
     * @return
     *      The relative path from the base directory to the specified file.
     */
    public static Path getRelativePath(final File file, final File baseDir) {
        final Path fileDirPath = file.toPath();
        final Path baseDirPath = baseDir.toPath();
        return baseDirPath.relativize(fileDirPath);
    }
}

Related

  1. getOptionalPathRelativeToMavenProjectRoot(File absoluteFile)
  2. getRelativeFilePathToCwd(File file)
  3. getRelativePath(File baseFile, File file, String resultIfImpossible)
  4. getRelativePath(File basePath, File path)
  5. getRelativePath(File root, File f)
  6. getRelativePath(Path file, Path baseDir)
  7. getRelativePath(Path rootPath, Path path)
  8. getRelativePath(String file, String directory)
  9. getRelativePath(String fullPath, String homeFolderPath)