Java Relative Path Get relativePath(final String inputPath, final File file)

Here you can find the source of relativePath(final String inputPath, final File file)

Description

relative Path

License

LGPL

Declaration

public static String relativePath(final String inputPath, final File file) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.*;

public class Main {
    public static String relativePath(final String inputPath, final File file) {
        String filePath = normalizePath(file.getAbsolutePath());
        String relativePath = filePath;
        if (filePath.contains("/")) {
            relativePath = filePath.substring(0, filePath.lastIndexOf("/"));
        }// ww  w.j av  a  2  s.c  o  m
        relativePath = relativePath.substring(normalizePath(inputPath).length());
        return relativePath;
    }

    public static String normalizePath(final String in) {
        String out = in;
        if (in.endsWith("/")) {
            out = in.substring(0, in.length() - 1);
        }
        return out.replace("\\", "/");
    }
}

Related

  1. relativePath(File parent, File child)
  2. relativePath(File path, File relativeTo)
  3. relativePath(File root, File node)
  4. relativePath(File src, File dest)
  5. relativePath(final File root, final File file)
  6. relativePath(String origin, String target)
  7. relativePath(String p_absolutePath, String p_currentPath)
  8. relativePathFiles(String relativePath, final String... fileNamesRegex)
  9. relativeTo(File peer, String filename)