Java File Path Create buildPath(String first, String... parts)

Here you can find the source of buildPath(String first, String... parts)

Description

Concatenate path parts into a full path, taking care of extra or missing slashes.

License

Mozilla Public License

Parameter

Parameter Description
first the first part.
parts the additional parts.

Return

the concatenated path.

Declaration

public static String buildPath(String first, String... parts) 

Method Source Code

//package com.java2s;
/**/*from w ww.  ja v a  2  s. c o m*/
 * Copyright (c) 2013-2016, The SeedStack authors <http://seedstack.org>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

public class Main {
    /**
     * Concatenate path parts into a full path, taking care of extra or missing slashes.
     *
     * @param first the first part.
     * @param parts the additional parts.
     * @return the concatenated path.
     */
    public static String buildPath(String first, String... parts) {
        String result = first;

        for (String part : parts) {
            if (result.isEmpty()) {
                if (part.startsWith("/")) {
                    result += part.substring(1);
                } else {
                    result += part;
                }
            } else {
                if (result.endsWith("/") && part.startsWith("/")) {
                    result += part.substring(1);
                } else if (!result.endsWith("/") && !part.startsWith("/") && !part.isEmpty()) {
                    result += "/" + part;
                } else {
                    result += part;
                }
            }
        }

        return result;
    }
}

Related

  1. buildPath(final boolean endWithSep, final String... val)
  2. buildPath(final String basePath, final String path)
  3. buildPath(final String... tokens)
  4. buildPath(Integer sAccountId, String objectPath)
  5. buildPath(long id, boolean justDir)
  6. buildPath(String part1, String part2)
  7. buildPath(String path, String file)
  8. buildPath(String... paths)
  9. buildPath(String... strings)