Java Path Relative Get getRelativePath(String fileName, String projFile)

Here you can find the source of getRelativePath(String fileName, String projFile)

Description

Get relative path of the file using project file path

License

Open Source License

Parameter

Parameter Description
fileName File path
projFile Project file path

Exception

Parameter Description

Return

Relative path

Declaration

public static String getRelativePath(String fileName, String projFile) throws IOException 

Method Source Code

//package com.java2s;
/* Copyright 2012 Yaqiang Wang,
* yaqiang.wang@gmail.com// w  w w . ja va2s. com
* 
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
* 
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
* General Public License for more details.
*/

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Get relative path of the file using project file path
     *
     * @param fileName File path
     * @param projFile Project file path
     * @return Relative path
     * @throws java.io.IOException
     */
    public static String getRelativePath(String fileName, String projFile) throws IOException {
        String RelativePath = "";
        File aFile = new File(fileName);
        File pFile = new File(projFile);
        fileName = aFile.getCanonicalPath();

        String layerPathRoot = getPathRoot(aFile);
        String projPathRoot = getPathRoot(pFile);
        if (!layerPathRoot.equalsIgnoreCase(projPathRoot)) {
            RelativePath = fileName;
        } else {
            List<String> aList = new ArrayList<>();
            aList.add(fileName);
            do {
                aList.add("");
                File tempFile = new File(aList.get(aList.size() - 2));
                if (tempFile.exists() && tempFile.getParent() != null) {
                    aList.set(aList.size() - 1, tempFile.getParent());
                } else {
                    break;
                }
            } while (!"".equals(aList.get(aList.size() - 1)));

            List<String> bList = new ArrayList<>();
            bList.add(pFile.getCanonicalPath());
            do {
                bList.add("");
                File tempFile = new File(bList.get(bList.size() - 2));
                if (tempFile.getParent() != null) {
                    bList.set(bList.size() - 1, tempFile.getParent());
                } else {
                    break;
                }
            } while (!"".equals(bList.get(bList.size() - 1)));

            boolean ifExist = false;
            int offSet;
            for (int i = 0; i < aList.size(); i++) {
                for (int j = 0; j < bList.size(); j++) {
                    if (aList.get(i).equals(bList.get(j))) {
                        for (int k = 1; k < j; k++) {
                            RelativePath = RelativePath + ".." + File.separator;
                        }
                        if (aList.get(i).endsWith(File.separator)) {
                            offSet = 0;
                        } else {
                            offSet = 1;
                        }
                        RelativePath = RelativePath + fileName.substring(aList.get(i).length() + offSet);
                        ifExist = true;
                        break;
                    }
                }
                if (ifExist) {
                    break;
                }
            }
        }

        if ("".equals(RelativePath)) {
            RelativePath = fileName;
        }
        return RelativePath;
    }

    /**
     * Get root path
     *
     * @param aFile The file
     * @return Root path
     */
    public static String getPathRoot(File aFile) {
        File path = aFile.getParentFile();
        String pathRoot = path.toString();
        while (path != null) {
            path = path.getParentFile();
            if (path != null) {
                pathRoot = path.toString();
            }
        }

        return pathRoot;
    }
}

Related

  1. getRelativePath(String base, String fullPath)
  2. getRelativePath(String base, String relative, boolean isBaseFile)
  3. getRelativePath(String baseFullPath, File file)
  4. getRelativePath(String basePath, String path)
  5. getRelativePath(String basePath, String pathToRelativize)
  6. getRelativePath(String filePath, String basePath)
  7. getRelativePath(String from, String to)
  8. getRelativePath(String parent, String child)
  9. getRelativePath(String path, String base)