Java Path Create makePath(String... elements)

Here you can find the source of makePath(String... elements)

Description

Construct a file path from the given elements, i.e.

License

Open Source License

Parameter

Parameter Description
elements The elements to create a path with

Return

The created path

Declaration

public static String makePath(String... elements) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.util.Arrays;

public class Main {
    /**/*ww  w .j a va  2 s. c  om*/
     * Construct a file path from the given elements, i.e. separate the given elements by the file separator.
     *
     * @param elements The elements to create a path with
     *
     * @return The created path
     */
    public static String makePath(String... elements) {
        return join(File.separator, elements);
    }

    /**
     * Join a list of elements into a single string with the specified delimiter.
     *
     * @param delimiter The delimiter to use
     * @param elements  The elements to join
     *
     * @return A new String that is composed of the elements separated by the delimiter
     */
    public static String join(String delimiter, Iterable<String> elements) {
        if (delimiter == null) {
            delimiter = "";
        }
        StringBuilder sb = new StringBuilder();
        for (String element : elements) {
            if (!isEmpty(element)) {
                // Add the separator if it isn't the first element
                if (sb.length() > 0) {
                    sb.append(delimiter);
                }
                sb.append(element);
            }
        }

        return sb.toString();
    }

    /**
     * Join a list of elements into a single string with the specified delimiter.
     *
     * @param delimiter The delimiter to use
     * @param elements  The elements to join
     *
     * @return A new String that is composed of the elements separated by the delimiter
     */
    public static String join(String delimiter, String... elements) {
        return join(delimiter, Arrays.asList(elements));
    }

    /**
     * Null-safe method for checking whether a string is empty. Note that the string
     * is trimmed, so this method also considers a string with whitespace as empty.
     *
     * @param str The string to verify
     *
     * @return True if the string is empty, false otherwise
     */
    public static boolean isEmpty(String str) {
        return str == null || str.trim().isEmpty();
    }
}

Related

  1. makePath(final String dir, final String... jars)
  2. makePath(String basePath)
  3. makePath(String filename, String savePath)
  4. makePath(String path1, String path2)
  5. makePath(String... elements)
  6. makePath(String[] strings)
  7. makePathAbsolute(String path)
  8. makePathASafeFileName(String filePath)
  9. makePathFile(String javaConnectorSourcePath, String packageName, String className)