Java Path Relative getRelativeFileName(File target, File realativeTo)

Here you can find the source of getRelativeFileName(File target, File realativeTo)

Description

Returns the name of one file relative to another.

License

Apache License

Declaration

public static String getRelativeFileName(File target, File realativeTo) 

Method Source Code

//package com.java2s;
/*/*from   w w w  .ja v a2s  . c o  m*/
 * Copyright 1999-2006 University of Chicago
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.util.LinkedList;

public class Main {
    /**
     * Returns the name of one file relative to another.  The name it returns
     * uses forward slashes "/" instead of the system specific file separator.
     */
    public static String getRelativeFileName(File target, File realativeTo) {
        LinkedList targetList = getPathList(target);
        LinkedList relativeList = getPathList(realativeTo);

        while (!targetList.isEmpty() && !relativeList.isEmpty()
                && targetList.getFirst().equals(relativeList.getFirst())) {
            targetList.removeFirst();
            relativeList.removeFirst();
        }

        StringBuffer fileName = new StringBuffer();

        while (!relativeList.isEmpty()) {
            fileName.append("../");
            relativeList.removeFirst();
        }

        while (!targetList.isEmpty()) {
            fileName.append(targetList.removeFirst());
            fileName.append('/');
        }

        fileName.append(target.getName());

        return fileName.toString();
    }

    private static LinkedList getPathList(File file) {
        if (!file.isAbsolute()) {
            file = file.getAbsoluteFile();
        }

        LinkedList list = new LinkedList();

        if (!file.isDirectory()) {
            file = file.getParentFile();
        }

        while (file != null) {
            if (file.getName().length() > 0) { //getName() returns "" for the root dir 
                list.addFirst(file.getName());
            }

            file = file.getParentFile();
        }

        return list;
    }
}

Related

  1. getRelativeFile(String baseDir, String fileName)
  2. getRelativeFileFromReference(String ref, File metadataFile)
  3. getRelativeFileInternal(File canonicalBaseFile, File canonicalFileToRelativize)
  4. getRelativeFilename(File base, File target)
  5. getRelativeFileName(File file, File basedir)
  6. getRelativeFileName(String filePath, String directoryPath)
  7. getRelativeFilePath(File absolutePath, File reference)
  8. getRelativeFilePath(String filePath, String relativePathPrefix)
  9. getRelativeLastModifiedTimeFile()