Java Path File Extention nio getExtension(Path path)

Here you can find the source of getExtension(Path path)

Description

get Extension

License

Open Source License

Parameter

Parameter Description
path The path to operate on.

Return

The extension of the path name, excluding the initial ".".

Declaration

public static String getExtension(Path path) 

Method Source Code

//package com.java2s;
/*/*  www.jav a2s  .  co  m*/
 * 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 operate on.
     * @return The extension of the path name, excluding the initial ".".
     */
    public static String getExtension(Path path) {
        return getExtension(path != null ? path.toString() : null);
    }

    /**
     * @param path The path to operate on.
     * @return The extension of the path name, excluding the initial ".".
     */
    public static final String getExtension(String path) {
        path = getLeafName(path);
        if (path != null) {
            int dot = path.lastIndexOf('.');
            if (dot != -1 && dot + 1 < path.length()) {
                return path.substring(dot + 1);
            }
        }
        return "";
    }

    /**
     * @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. fileRenamer(final String fromPath, final String toPath, @Nullable final String toExtension, final boolean junkPaths)
  2. findFilesWithExtension(Path directory, String targetExtension)
  3. getAllPaths(Path root, final String... extensions)
  4. getExtension(final Path path)
  5. getExtension(Path file)
  6. getFileExtension(final Path file)
  7. getFileExtension(final Path path)
  8. getFileExtension(Path file)
  9. getFileextension(Path path)