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