Java Path Relative getRelativeFilePath(String filePath, String relativePathPrefix)

Here you can find the source of getRelativeFilePath(String filePath, String relativePathPrefix)

Description

Get relative path that follows relativePathPrefix .

License

Open Source License

Parameter

Parameter Description
filePath a parameter
relativePathPrefix a parameter

Return

relative path if relativePathPrefix is found; otherwise, absolutePath.

Declaration

public static String getRelativeFilePath(String filePath, String relativePathPrefix) 

Method Source Code


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

public class Main {
    /**// w w w .  ja  va  2 s .  co m
     * Get relative path that follows {@code relativePathPrefix}. If {@code relativePathPrefix} is
     * not found in {@code filePath}, then filePath is returned.
     * 
     * @param filePath
     * @param relativePathPrefix
     * @return relative path if relativePathPrefix is found; otherwise, absolutePath.
     */
    public static String getRelativeFilePath(String filePath, String relativePathPrefix) {
        int prefixIndex = filePath.indexOf(relativePathPrefix);
        if (prefixIndex == -1)
            return filePath;

        int relativePathStartIndex = prefixIndex + relativePathPrefix.length() + 1;
        return filePath.substring(relativePathStartIndex);
    }

    /**
     * Returns a relative path to the input file. This method assumes a certain directory structure
     * and returns a relative path starting at the {@code relativePathPrefix} directory.
     * 
     * @param file
     * @return
     */
    public static String getRelativeFilePath(File file, String relativePathPrefix) {
        if (relativePathPrefix == null)
            return file.toURI().toString();

        return getRelativeFilePath(file.getAbsolutePath(), relativePathPrefix);
    }
}

Related

  1. getRelativeFilename(File base, File target)
  2. getRelativeFileName(File file, File basedir)
  3. getRelativeFileName(File target, File realativeTo)
  4. getRelativeFileName(String filePath, String directoryPath)
  5. getRelativeFilePath(File absolutePath, File reference)
  6. getRelativeLastModifiedTimeFile()
  7. getRelativeLink(File target, File base)
  8. getRelativeName(File file, File root)
  9. getRelativeName(final File directory, final File file)