Java Path File Extention nio getFileExtension(final Path path)

Here you can find the source of getFileExtension(final Path path)

Description

Return the extension of the file.
Strip all the parent folders and the name of the file to return just the file extension.

License

Open Source License

Parameter

Parameter Description
path the file, it must not be a directory.

Return

the file extension.

Declaration

public static String getFileExtension(final Path path) 

Method Source Code

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

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;

public class Main {
    /** The character used to separate the filename from its extension */
    public static final Character FILE_EXTENSION_SEPARATOR = '.';

    /**//from  w ww  .j a  v a 2s.  c  om
     * Return the extension of the file.<br />
     * Strip all the parent folders and the name of the file to return just the file extension.
     * <strong>Note that for Unix hidden files (starting with '.'), this method return an empty string.</strong>
     * @param path
     *        the file, it must not be a directory.
     * @return the file extension.
     */
    public static String getFileExtension(final Path path) {
        Objects.requireNonNull(path);
        if (Files.isDirectory(path)) {
            throw new IllegalArgumentException("Cannot get extension of a directory");
        }
        final String filename = path.getFileName().toString();
        final int extensionSeparatorIndex = filename.lastIndexOf(FILE_EXTENSION_SEPARATOR);
        if (extensionSeparatorIndex <= 0) {
            return "";
        }
        return filename.substring(extensionSeparatorIndex + 1);
    }

    /**
     * Return the raw filename of the specified path.<br />
     * Strip all the parent folders and the extension of the file to just return the filename.
     * <strong>Note that for Unix hidden files (starting with '.'), this method return the full
     * filename.</strong>
     * @param path
     *        the file, it must not be a directory.
     * @return the filename, without the folders and the extension.
     */
    public static String getFilename(final Path path) {
        Objects.requireNonNull(path);
        if (Files.isDirectory(path)) {
            throw new IllegalArgumentException("Cannot get name of a directory");
        }
        final String filename = path.getFileName().toString();
        final int extensionSeparatorIndex = filename.lastIndexOf(FILE_EXTENSION_SEPARATOR);
        if (extensionSeparatorIndex <= 0 || extensionSeparatorIndex == filename.length() - 1) {
            return filename;
        }
        return filename.substring(0, extensionSeparatorIndex);
    }
}

Related

  1. getAllPaths(Path root, final String... extensions)
  2. getExtension(final Path path)
  3. getExtension(Path file)
  4. getExtension(Path path)
  5. getFileExtension(final Path file)
  6. getFileExtension(Path file)
  7. getFileextension(Path path)
  8. getFileExtension(Path path)
  9. getFileExtension(Path path)