Java Path Combine combinePath(String path1, String path2, String pathSeperator)

Here you can find the source of combinePath(String path1, String path2, String pathSeperator)

Description

combine Path

License

Open Source License

Parameter

Parameter Description
path1 a parameter
path2 a parameter
pathSeperator a parameter

Declaration

public static String combinePath(String path1, String path2,
        String pathSeperator) 

Method Source Code

//package com.java2s;
/*// w  w  w. j a  v  a  2 s . c om
 * Title        :  AlchemiXmlUtil.java
 * Package      :  org.gridbus.alchemi.client.util
 * Project      :  AlchemiJavaAPI
 * Description   :  
 * Created on   :  4/08/2005
 * Author      :  Krishna Nadiminti (kna@cs.mu.oz.au)
 * Copyright    :  (c) 2005, Grid Computing and Distributed Systems Laboratory, 
 *                   Dept. of Computer Science and Software Engineering,
 *                   University of Melbourne, Australia.
 * 
 * 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 2 of the License, or (at your option) any later  version.
 * See the GNU General Public License (http://www.gnu.org/copyleft/gpl.html)for more details.
 * 
 */

import java.io.File;

public class Main {
    /**
     * @param path1
     * @param path2
     * @param pathSeperator
     * @return
     */
    public static String combinePath(String path1, String path2,
            String pathSeperator) {
        String path = null;
        //make sure there is a valid path seperator char. / or \
        if (pathSeperator == null || pathSeperator.trim().equals("")) {
            pathSeperator = File.separator;
        }
        if (path1 != null && path2 != null) {
            path1 = path1.trim();
            path2 = path2.trim();

            if (path1.equals(""))
                return path2;

            if (path2.equals(""))
                return path1;

            if (path1.endsWith(pathSeperator)) {
                path = path1.substring(0,
                        path1.length() - pathSeperator.length())
                        + pathSeperator + path2;
            } else {
                path = path1 + pathSeperator + path2;
            }
            if (path.endsWith(pathSeperator)) {
                path = path.substring(0,
                        path.length() - pathSeperator.length());
            }
        }
        return path;
    }
}

Related

  1. combinePath(String basePath, String withPath)
  2. combinePath(String parent, String child)
  3. combinePath(String parent, String name)
  4. combinePath(String part0, String... parts)
  5. combinePath(String path1, String path2)
  6. combinePath(String... paths)
  7. combinePath(String... paths)
  8. combinePaths(final String path, final String... furtherPaths)
  9. combinePaths(String root, String... more)