Java File Path Create buildPath(final boolean endWithSep, final String... val)

Here you can find the source of buildPath(final boolean endWithSep, final String... val)

Description

Build a path out of the given elements.

License

Apache License

Parameter

Parameter Description
endWithSep a parameter
val - list of elements and path separators.

Return

completed path

Declaration

public static String buildPath(final boolean endWithSep, final String... val) 

Method Source Code

//package com.java2s;
/* ********************************************************************
Licensed to Jasig under one or more contributor license
agreements. See the NOTICE file distributed with this work
for additional information regarding copyright ownership.
Jasig licenses this file to you 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://from   w  w w  .  j a v  a 2  s .c o m
    
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.
*/

public class Main {
    /** Build a path out of the given elements. Any parameter may be a path element
     * or a path separator. Note this only allows "/" as the separator.
     *
     * <p>The path is constrained to either end or not end with the path separator.
     * The path will be adjusted according to the constraint
     *
     * @param endWithSep
     * @param val - list of elements and path separators.
     * @return completed path
     */
    public static String buildPath(final boolean endWithSep, final String... val) {
        StringBuilder path = new StringBuilder();

        for (String s : val) {
            if (s != null) {
                path.append(s);
            }
        }

        String s = path.toString().replaceAll("/+", "/");

        if (endWithSep) {
            if (!s.endsWith("/")) {
                s += "/";
            }
        } else if (s.endsWith("/")) {
            s = s.substring(0, s.length() - 1);
        }

        return s;
    }
}

Related

  1. buildPath(final String basePath, final String path)
  2. buildPath(final String... tokens)
  3. buildPath(Integer sAccountId, String objectPath)
  4. buildPath(long id, boolean justDir)