Here you can find the source of getFilename(final Path path)
Parameter | Description |
---|---|
path | the file, it must not be a directory. |
public static String getFilename(final Path path)
//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); } }