Java File Path Create buildPath(final String basePath, final String path)

Here you can find the source of buildPath(final String basePath, final String path)

Description

build Path

License

Open Source License

Declaration

public static String buildPath(final String basePath, final String path) 

Method Source Code

//package com.java2s;
/*//from   w  w w  . j  a  va 2  s . c o  m
 * ******************************************************************************
 *   Copyright 2014-2017 Spectra Logic Corporation. All Rights Reserved.
 *   Licensed under the Apache License, Version 2.0 (the "License"). You may not use
 *   this file except in compliance with the License. A copy of the License is located at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   or in the "license" file accompanying this file.
 *   This file 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.
 * ****************************************************************************
 */

public class Main {
    public static String buildPath(final String basePath, final String path) {
        //Make sure to have some guard statements for null.
        if (basePath == null && path == null) {
            return "";
        }

        if (basePath == null) {
            return path;
        }

        if (path == null) {
            return basePath;
        }

        final StringBuilder builder = new StringBuilder();
        if (!basePath.startsWith("/")) {
            builder.append('/');
        }
        builder.append(basePath);

        if (!(path.startsWith("/") || basePath.endsWith("/"))) {
            builder.append('/');
        }

        if (path.startsWith("/") && basePath.endsWith("/")) {
            builder.append(path.substring(1));
        } else {
            builder.append(path);
        }

        return builder.toString();
    }
}

Related

  1. buildPath(final boolean endWithSep, final String... val)
  2. buildPath(final String... tokens)
  3. buildPath(Integer sAccountId, String objectPath)
  4. buildPath(long id, boolean justDir)
  5. buildPath(String first, String... parts)