Java Path Combine combinePaths(String root, String... more)

Here you can find the source of combinePaths(String root, String... more)

Description

Similar to Paths.get(String, String...) in Java 7's NIO2 Package.

License

Open Source License

Parameter

Parameter Description
root - Root Path
more - Children to Append

Declaration

public static final String combinePaths(String root, String... more) 

Method Source Code


//package com.java2s;
/*/*from   ww  w  .  jav a 2 s .  co m*/
 * Argus Installer v2 -- A Better School Zip Alternative Copyright (C) 2014 Matthew
 * Crocco
 * 
 * This program is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this
 * program. If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

public class Main {
    /**
     * Similar to Paths.get(String, String...) in Java 7's NIO2 Package. <br />
     * <br />
     * This method will combine a root path with multiple children to create a <br />
     * a single path with proper separator characters.
     * 
     * @param root
     *            - Root Path
     * @param more
     *            - Children to Append
     * @return
     */
    public static final String combinePaths(String root, String... more) {
        final StringBuilder sb = new StringBuilder(root);

        // Remove final separator if it exists to simplify appending
        if (root.endsWith("/") || root.endsWith("\\")) {
            sb.deleteCharAt(sb.length() - 1);
        }

        // Append Children to Root Path
        for (final String s : more) {
            sb.append(File.separator + s);
        }

        // Ensure Path Separator's are Homogeneous
        for (int i = 0; i < sb.length(); i++) {
            if ((sb.charAt(i) == '/') || (sb.charAt(i) == '\\')) {
                sb.setCharAt(i, File.separatorChar);
            }
        }

        return sb.toString();
    }
}

Related

  1. combinePath(String path1, String path2, String pathSeperator)
  2. combinePath(String... paths)
  3. combinePath(String... paths)
  4. combinePaths(final String path, final String... furtherPaths)
  5. combinePaths(String root, String... more)
  6. combinePaths(String... items)
  7. combinePaths(String[] paths)