Java Path Relative getRelativeFile(File target, File baseDirectory)

Here you can find the source of getRelativeFile(File target, File baseDirectory)

Description

From <a href="http://stackoverflow.com/a/1269907">http://stackoverflow.com /a/1269907</a>.

License

Apache License

Parameter

Parameter Description
target File to relativize
baseDirectory Base directory to relativize to (treated as a directory: /dir/file.txt treated as /dir/file.txt/)

Exception

Parameter Description
IOException If an IOException occurs when callingFile#getCanonicalPath()

Return

Relativized file

Declaration

public static File getRelativeFile(File target, File baseDirectory) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2013 Geoscience Australia//from   w  w  w  .  j ava 2  s.  c  o  m
 * 
 * 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.io.IOException;

import java.util.regex.Pattern;

public class Main {
    /**
     * From <a
     * href="http://stackoverflow.com/a/1269907">http://stackoverflow.com
     * /a/1269907</a>.
     * 
     * @param target
     *            File to relativize
     * @param baseDirectory
     *            Base directory to relativize to (treated as a directory:
     *            /dir/file.txt treated as /dir/file.txt/)
     * @return Relativized file
     * @throws IOException
     *             If an IOException occurs when calling
     *             {@link File#getCanonicalPath()}
     */
    public static File getRelativeFile(File target, File baseDirectory) throws IOException {
        String[] baseComponents = baseDirectory.getCanonicalPath().split(Pattern.quote(File.separator));
        String[] targetComponents = target.getCanonicalPath().split(Pattern.quote(File.separator));

        // skip common components
        int index = 0;
        for (; index < targetComponents.length && index < baseComponents.length; ++index) {
            if (!targetComponents[index].equals(baseComponents[index]))
                break;
        }

        StringBuilder result = new StringBuilder();
        if (index != baseComponents.length) {
            // backtrack to base directory
            for (int i = index; i < baseComponents.length; ++i)
                result.append(".." + File.separator);
        }
        for (; index < targetComponents.length; ++index)
            result.append(targetComponents[index] + File.separator);
        if (!target.getPath().endsWith("/") && !target.getPath().endsWith("\\")) {
            // remove final path separator
            result.delete(result.length() - File.separator.length(), result.length());
        }
        return new File(result.toString());
    }
}

Related

  1. getRelativeDirectoryPath(String in_filePath, String in_basePath)
  2. getRelativeFile(File parent, final String s2)
  3. getRelativeFile(File source, String path)
  4. getRelativeFile(File subj, File relativeTo)
  5. getRelativeFile(File target, File base)
  6. getRelativeFile(File targetFile, File baseFile)
  7. getRelativeFile(java.io.File srcFile, int upCount, String... childPaths)
  8. getRelativeFile(String baseDir, String fileName)
  9. getRelativeFileFromReference(String ref, File metadataFile)