Java Utililty Methods Relative Path Get

List of utility methods to do Relative Path Get

Description

The list of methods to do Relative Path Get are organized into topic(s).

Method

StringrelativePath(String p_absolutePath, String p_currentPath)
relative Path
int index = p_currentPath.length();
if (!p_currentPath.endsWith(File.separator)) {
    index++;
return p_absolutePath.substring(index);
CollectionrelativePathFiles(String relativePath, final String... fileNamesRegex)
relative Path Files
relativePath = relativePath.trim();
File dir = new File(relativePath).getAbsoluteFile();
File[] jars = dir.listFiles(new FileFilter() {
    public boolean accept(File pathname) {
        if (pathname.isFile()) {
            String fileName = pathname.getName();
            for (String regex : fileNamesRegex) {
                if (fileName.matches(regex)) {
...
FilerelativeTo(File peer, String filename)
relative To
File filenameFile = new File(filename);
if (filenameFile.isAbsolute()) {
    return filenameFile;
File dir = peer.isDirectory() ? peer : peer.getParentFile();
return new File(dir, filename).getAbsoluteFile();
StringrelativeToAbsoluteFilePath(String s)
From relative path to a like canonical path Added by JG 2015
if (s.matches("^\\.\\/.*")) {
    File f = new File(s);
    s = f.getAbsolutePath();
    s = s.replaceAll(File.separator + "\\." + File.separator, File.separator);
return s;
Stringrelativize(File base, File absolute)
relativize
String relative = base.toURI().relativize(absolute.toURI()).getPath();
return relative;
Filerelativize(File base, File child)
Makes a file path relative to the base
return new File(base.toURI().relativize(child.toURI()).getPath());
Stringrelativize(File file, File basedir)
relativize
if (file == null) {
    return null;
if (basedir == null) {
    basedir = new File("").getAbsoluteFile();
} else if (!basedir.isAbsolute()) {
    basedir = basedir.getAbsoluteFile();
String pathname;
String basePath = basedir.getPath();
String filePath = file.getPath();
if (filePath.startsWith(basePath)) {
    if (filePath.length() == basePath.length()) {
        pathname = "";
    } else if (basePath.endsWith(File.separator)) {
        pathname = filePath.substring(basePath.length());
    } else if (filePath.charAt(basePath.length()) == File.separatorChar) {
        pathname = filePath.substring(basePath.length() + 1);
    } else {
        pathname = null;
} else {
    pathname = "";
    for (File current = file; !basedir.equals(current);) {
        String filename = current.getName();
        current = current.getParentFile();
        if (current == null) {
            return null;
        if (pathname.length() > 0) {
            pathname = filename + File.separatorChar + pathname;
        } else {
            pathname = filename;
return pathname;
Stringrelativize(File home, File f)
get relative path of File 'f' with respect to 'home' directory
 example : home = /a/b/c f    = /a/d/e/x.txt s = getRelativePath(home,f) = ../../d/e/x.txt 
Author: David M.
List<String> homelist = getPathList(home);
List<String> filelist = getPathList(f);
String s = matchPathLists(homelist, filelist);
return s;
Filerelativize(File path, File relative)
relativize
if (!relative.isDirectory()) {
    relative = relative.getParentFile();
String aPath = (path.isDirectory() ? path : path.getParentFile()).getCanonicalPath();
String aRel = relative.getCanonicalPath();
aPath = fixSeparatorChars(aPath);
aRel = fixSeparatorChars(aRel);
String[] aPaths = aPath.split("/");
...
Stringrelativize(final File root, final File target)
Calculates the relative path between a specified root directory and a target path.
if (root == null)
    throw new IllegalArgumentException("The root file cannot be null.");
if (target == null)
    throw new IllegalArgumentException("The target cannot be null.");
if (!root.isDirectory())
    throw new IllegalArgumentException("The root file must be a directory.");
if (!root.isAbsolute())
    throw new IllegalArgumentException("The root file must be absolute.");
...