Apply the given relative path to the given path, assuming standard Java folder separation (i.e. - Java java.io

Java examples for java.io:Path Name

Description

Apply the given relative path to the given path, assuming standard Java folder separation (i.e.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String path = "java2s.com";
        String relativePath = "java2s.com";
        System.out.println(applyRelativePath(path, relativePath));
    }/*from  www.  j  av a 2 s.  com*/

    private static final String FOLDER_SEPARATOR = "/";

    /**
     * Apply the given relative path to the given path, assuming standard Java folder separation (i.e. "/"
     * separators).
     *
     * @param path
     *            the path to start from (usually a full file path)
     * @param relativePath
     *            the relative path to apply (relative to the full file path above)
     * @return the full file path that results from applying the relative path
     */
    public static String applyRelativePath(final String path,
            final String relativePath) {

        final int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR);
        if (separatorIndex != -1) {
            String newPath = path.substring(0, separatorIndex);
            if (!relativePath.startsWith(FOLDER_SEPARATOR)) {
                newPath += FOLDER_SEPARATOR;
            }
            return newPath + relativePath;
        } else
            return relativePath;
    }
}

Related Tutorials