Java Path Relative Get getRelativePath(File rootDirectory, File file)

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

Description

Returns the path of file relative to rootDirectory .

License

Open Source License

Declaration

public static String getRelativePath(File rootDirectory, File file) 

Method Source Code


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

import java.io.File;
import java.io.IOException;

public class Main {
    /** Returns the path of {@code file} relative to {@code rootDirectory}. */
    public static String getRelativePath(File rootDirectory, File file) {
        try {/* w  ww . j av  a2s  .  co  m*/
            String rootDir = rootDirectory.getCanonicalPath() + File.separator;
            String f = file.getCanonicalPath();
            if (f.startsWith(rootDir)) {
                return f.substring(rootDir.length());
            }
        } catch (IOException e) {
            // fall back to file.getPath()
        }
        return file.getPath();
    }
}

Related

  1. getRelativePath(File path, File basePath)
  2. getRelativePath(File ref_file, File tst_file)
  3. getRelativePath(File root, File file)
  4. getRelativePath(File root, File file)
  5. getRelativePath(File root, File target, String fileSeparator)
  6. getRelativePath(File source, File destination)
  7. getRelativePath(File source, File target)
  8. getRelativePath(File src, File dst)
  9. getRelativePath(File subj, File relativeTo)