Java Path Relative nio getRelativePath(String fullPath, String homeFolderPath)

Here you can find the source of getRelativePath(String fullPath, String homeFolderPath)

Description

Return the path of the folder as in the database entry of the file

License

Open Source License

Parameter

Parameter Description
fullPath a parameter
homeFolderPath a parameter

Declaration

public static String getRelativePath(String fullPath, String homeFolderPath) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.file.Paths;

public class Main {
    /**/*from   w  ww . java 2s  . c o m*/
     * Return the path of the folder as in the database entry of the file
     * 
     * @param fullPath
     * @param homeFolderPath
     * @return
     */
    public static String getRelativePath(String fullPath, String homeFolderPath) {

        String parentPath = "";
        try {
            URL parentURL = Paths.get(fullPath).toUri().toURL();
            URL homeFolderURL = Paths.get(homeFolderPath).toUri().toURL();

            parentPath = parentURL.getPath().replace(homeFolderURL.getPath(), "");

            // Decode the URL
            parentPath = URLDecoder.decode(parentPath, "UTF-8");

            // Remove last slash of the string
            if (parentPath.length() > 0 && parentPath.charAt(parentPath.length() - 1) == '/') {
                parentPath = parentPath.substring(0, parentPath.length() - 1);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return parentPath;
    }
}

Related

  1. getRelativePath(File root, File f)
  2. getRelativePath(final File file, final File baseDir)
  3. getRelativePath(Path file, Path baseDir)
  4. getRelativePath(Path rootPath, Path path)
  5. getRelativePath(String file, String directory)
  6. getRelativePath(String relativePathFile)
  7. getSourceFileRelativePath(Class declaringClass)
  8. isRelativePath(Path baseDir, Path file)
  9. isRelativized(final Path base, final String successor)