Java Path Relative nio getRelativePath(File root, File f)

Here you can find the source of getRelativePath(File root, File f)

Description

get Relative Path

License

Open Source License

Declaration

public static String getRelativePath(File root, File f) 

Method Source Code


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

import java.io.File;

import java.nio.file.Path;

public class Main {
    public static String getRelativePath(File root, File f) {
        Path fPath = f.toPath().toAbsolutePath();
        Path rootPath = root.toPath().toAbsolutePath();
        if (fPath.startsWith(rootPath)) {
            return fPath.toString().substring(rootPath.toString().length() + 1);
        } else {//  ww w . j av  a  2s . c  o  m
            throw new IllegalStateException(fPath + " must be located inside " + rootPath);
        }
    }
}

Related

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