Java Path Relative nio getRelativePath(File baseFile, File file, String resultIfImpossible)

Here you can find the source of getRelativePath(File baseFile, File file, String resultIfImpossible)

Description

get Relative Path

License

Apache License

Declaration

public static String getRelativePath(File baseFile, File file, String resultIfImpossible) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
    public static String getRelativePath(File baseFile, File file, String resultIfImpossible) {
        Path pathBase = Paths.get(baseFile.getAbsolutePath());
        Path pathAbsolute = Paths.get(file.getAbsolutePath());
        try {//from  w w  w  .  j a  v  a 2 s .  c o  m
            Path pathRelative = pathBase.relativize(pathAbsolute);
            return pathRelative.toString();
        } catch (Exception e) {
            return resultIfImpossible;
        }
    }
}

Related

  1. getChildEntryRelativePath(Path base, Path child, boolean convertToLinuxPath)
  2. getFileFromBaseFileAndPathThatMayBeRelative(File baseDir, String pathThatMayBeRelative)
  3. getFileRelativeFolders(Path basePath, Path filePath)
  4. getOptionalPathRelativeToMavenProjectRoot(File absoluteFile)
  5. getRelativeFilePathToCwd(File file)
  6. getRelativePath(File basePath, File path)
  7. getRelativePath(File root, File f)
  8. getRelativePath(final File file, final File baseDir)
  9. getRelativePath(Path file, Path baseDir)