Java Path Relative Get getRelativePath(File file, String xmlDirectoryPath)

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

Description

Returns a part of absolute path that starts from the xml directory path (xml directory path is cut off).

License

Open Source License

Parameter

Parameter Description
file a parameter
xmlDirectoryPath a parameter

Declaration

public static String getRelativePath(File file, String xmlDirectoryPath) 

Method Source Code


//package com.java2s;
import java.io.File;

public class Main {
    /**//from   w  w  w .j  av  a 2  s .co  m
     * Returns a part of absolute path that starts from the xml directory path (xml directory path is cut
     * off).
     * 
     * @param file
     * @param xmlDirectoryPath
     * @return
     */
    public static String getRelativePath(File file, String xmlDirectoryPath) {
        return removeLeadingSlashes(file.getAbsolutePath().substring(xmlDirectoryPath.length()));
    }

    /**
     * Removes leading slashs (one or more) if they are there. Leaves leading slash if link is only the
     * leading slash ("/").
     * 
     * @param link
     * @return
     */
    public static String removeLeadingSlashes(String link) {
        if (link == null)
            return "";

        if (link.equals("/"))
            return link;

        return link.startsWith("/") ? removeLeadingSlashes(link.substring(1)) : link;
    }
}

Related

  1. getRelativePath(File file, File directory)
  2. getRelativePath(File file, File folder)
  3. getRelativePath(File file, File root)
  4. getRelativePath(File file, File root)
  5. getRelativePath(File file, File srcDir)
  6. getRelativePath(File fileOrFolder, File baseFolder)
  7. getRelativePath(File folderContainingFile, File fileInFolder)
  8. getRelativePath(File fromDir, File toFile)
  9. getRelativePath(File fromFile, File toFile)