Java Path File Extention nio getLeafName(Path path, boolean includeExtension)

Here you can find the source of getLeafName(Path path, boolean includeExtension)

Description

get Leaf Name

License

Open Source License

Parameter

Parameter Description
path The path to process.
includeExtension Pass in <code>true</code> to leave the extension on the name or <code>false</code> to strip it off.

Return

The leaf portion of the path name (everything to the right of the last path separator).

Declaration

public static final String getLeafName(Path path, boolean includeExtension) 

Method Source Code

//package com.java2s;
/*//from w  ww  .  j a  va  2  s  .  c  om
 * Copyright (c) 1998-2017 by Richard A. Wilkes. All rights reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, version 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/.
 *
 * This Source Code Form is "Incompatible With Secondary Licenses", as
 * defined by the Mozilla Public License, version 2.0.
 */

import java.nio.file.Path;

public class Main {
    /**
     * @param path The path to process.
     * @return The leaf portion of the path name (everything to the right of the last path
     *         separator).
     */
    public static final String getLeafName(String path) {
        return getLeafName(path, true);
    }

    /**
     * @param path The path to process.
     * @param includeExtension Pass in <code>true</code> to leave the extension on the name or
     *            <code>false</code> to strip it off.
     * @return The leaf portion of the path name (everything to the right of the last path
     *         separator).
     */
    public static final String getLeafName(Path path, boolean includeExtension) {
        return getLeafName(path.toString(), includeExtension);
    }

    /**
     * @param path The path to process.
     * @param includeExtension Pass in <code>true</code> to leave the extension on the name or
     *            <code>false</code> to strip it off.
     * @return The leaf portion of the path name (everything to the right of the last path
     *         separator).
     */
    public static final String getLeafName(String path, boolean includeExtension) {
        if (path != null) {
            int index;

            path = path.replace('\\', '/');
            index = path.lastIndexOf('/');
            if (index != -1) {
                if (index == path.length() - 1) {
                    return "";
                }
                path = path.substring(index + 1);
            }

            if (!includeExtension) {
                index = path.lastIndexOf('.');
                if (index != -1) {
                    path = path.substring(0, index);
                }
            }
            return path;
        }
        return null;
    }
}

Related

  1. getFileNameWithoutExtension(Path path)
  2. getFileNameWithoutExtension(Path path)
  3. getFileNameWithoutExtension(String filePath)
  4. getFilesList(Path directory, Set extensions)
  5. getFullNameWithoutExtension(Path f)
  6. getListOfFilesByExtension(Path directoryPath, Set extensions)
  7. getPath(String outputDir, String qualifiedFilename, String fileextension)
  8. getTransformedOutputPath(Path input, String compressExtension, String outputDir)
  9. hasExtensionIgnoreCase(Path path, String ext)