Java Path Relative Get getRelativePath(File fromFile, File toFile)

Here you can find the source of getRelativePath(File fromFile, File toFile)

Description

get Relative Path

License

Open Source License

Declaration

public static String getRelativePath(File fromFile, File toFile) 

Method Source Code


//package com.java2s;

import java.io.*;

public class Main {
    public static String getRelativePath(File fromFile, File toFile) {
        File fromDir = fromFile.isDirectory() ? fromFile : fromFile.getParentFile();

        File toDir = toFile.isDirectory() ? toFile : toFile.getParentFile();

        File dir = fromDir;/* w w  w  . j a  v a 2 s .  c  o  m*/

        String relPath = "";

        while (dir != null && !dir.equals(toDir)) {
            relPath += "../";
            dir = dir.getParentFile();
        }

        if (dir == null) {
            throw new RuntimeException(toFile + " is not in parent of " + fromFile);
        }
        return relPath + toFile.toString();
    }
}

Related

  1. getRelativePath(File folderContainingFile, File fileInFolder)
  2. getRelativePath(File fromDir, File toFile)
  3. getRelativePath(File fromFile, File toFile)
  4. getRelativePath(File fromFile, File toFile)
  5. getRelativePath(File fromFile, File toFile)
  6. getRelativePath(File fromFolder, File toFile)
  7. getRelativePath(File home, File f)
  8. getRelativePath(File home, File f)
  9. getRelativePath(File home, File f)