Java Path File Name nio getFilename(final Path path)

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

Description

Return the raw filename of the specified path.
Strip all the parent folders and the extension of the file to just return the filename.

License

Open Source License

Parameter

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

Return

the filename, without the folders and the extension.

Declaration

public static String getFilename(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 = '.';

    /**/*  www  . j a v  a2  s. co m*/
     * 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. getDirectoryNames(Path baseDirectory)
  2. getFileContent(String fileNameOrPath)
  3. getFileInFolder(Path p, String filename)
  4. getFilename (final String filepath)
  5. getFilename(final Path path)
  6. getFileName(Path path)
  7. getFileName(Path path)
  8. getFileName(String fullPath, boolean withParentFolder)
  9. getFileName(String path)