The list of methods to do Path Relative are organized into topic(s).
String
getRelative(File base, File path) Get the relative path of a file using a base path to specify the absolute portion.
String absBase = base.getAbsolutePath();
String absPath = path.getAbsolutePath();
if (absPath.indexOf(absBase) != 0)
return absPath;
return absPath.substring(absBase.length() + (absPath.equals(absBase) ? 0 : 1));
String
getRelative(String basedir, String child) Given a basedir and a child file, return the relative path to the child.
if (basedir.endsWith("/") || basedir.endsWith("\\")) {
basedir = basedir.substring(0, basedir.length() - 1);
if (child.startsWith(basedir)) {
return child.substring(basedir.length() + 1);
String absoluteBasedir = new File(basedir).getAbsolutePath();
if (child.startsWith(absoluteBasedir)) {
...
String
getRelativeAncestor(File ancestor, File descendant) Get a relative path pointing from a descendant to an ancestor.
List ancestors = getAncestors(descendant);
if (!ancestors.contains(ancestor)) {
throw new IllegalArgumentException("The file \"" + descendant.getPath() + "\" "
+ "does not have ancestor \"" + ancestor.getPath() + "\".");
Iterator i = ancestors.iterator();
File f;
do {
...
String
getRelativeChildDirectory(String parent, String child) Given 2 paths, make sure parent and child are on the same tree return the port of child that not in parent
try {
String childPath = new File(child).getCanonicalFile().getPath().replace('\\', '/');
String parentPath = new File(parent).getCanonicalFile().getPath().replace('\\', '/');
if (!childPath.startsWith(parentPath)) {
throw new IllegalStateException();
String retDir = "." + childPath.substring(parentPath.length());
return retDir;
...
File
getRelativeFile(File parent, final String s2) get Relative File
File sf2 = new File(s2);
if (sf2.isAbsolute()) {
return sf2;
if (!parent.isDirectory()) {
parent = parent.getParentFile();
File file = new File(parent, s2);
...