Java Path Relative nio relativizePath(String basePath, File toRelativize)

Here you can find the source of relativizePath(String basePath, File toRelativize)

Description

This method has been deprecated use the Java Path relativize method instead.

License

Apache License

Parameter

Parameter Description
basePath The base directory path to remove from the file path.
toRelativize The file to be made relative.

Return

The relative file path created by removing the base directory. Returns null if the file to relative is null.

Declaration

@Deprecated
public static String relativizePath(String basePath, File toRelativize) 

Method Source Code


//package com.java2s;
/*//from w w w.  ja  va  2 s .c  o m
 * Copyright 2012 Johns Hopkins University
 *
 * 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.nio.file.FileSystems;
import java.nio.file.Path;

public class Main {
    /**
     * This method has been deprecated use the Java Path relativize method instead.
     * Strips the base directory out of a file path to make a relative file path. If the file doesn't contain the base path the original path is returned.
     * @param basePath The base directory path to remove from the file path.
     * @param toRelativize The file to be made relative.
     * @return The relative file path created by removing the base directory. Returns null if the file to relative is null.
     */
    @Deprecated
    public static String relativizePath(String basePath, File toRelativize) {

        if (toRelativize == null) {
            return null;
        }

        if (basePath == null || !toRelativize.getPath().startsWith(basePath)) {
            return toRelativize.getPath();
        }

        Path path = FileSystems.getDefault().getPath(basePath);
        Path relativePath = path.relativize(toRelativize.toPath());

        return relativePath.toString();
    }
}

Related

  1. relativize(Path target, Collection paths)
  2. relativizeAndNormalizePath(final String baseDirectory, final String path)
  3. relativizePath(IPath path, IPath basePath)
  4. relativizePath(IPath path, IPath basePath, boolean strict)
  5. relativizePath(Path root, Path child)
  6. resolvePathIfRelativeTo(File referenceFile, String partialPath)
  7. resolveRelative(Path p, String other)
  8. resolveRelativePath(String first, String... more)
  9. resolveRelativeRemotePath(Path root, Path file)