/** * Returns the index of the last extension separator character, which is a dot. This method also checks that there is no directory separator after the last * dot. To do this it uses {@link #indexOfLastSeparator(String)} which will handle a file in either Unix or Windows format. The output will be the same * irrespective of the machine that the code is running on. * * @param filename the filename to find the last path separator in, null returns -1 * @return the index of the last separator character, or -1 if there is no such character */ public static int indexOfExtension(String filename) { if (filename == null) { return -1; } int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR); int lastSeparator = indexOfLastSeparator(filename); return lastSeparator > extensionPos ? -1 : extensionPos; }
/** * Gets the name minus the path from a full filename. This method will handle a file in either Unix or Windows format. The text after the last forward or * backslash is returned. * <pre> * a/b/c.txt --> c.txt * a.txt --> a.txt * a/b/c --> c * a/b/c/ --> "" * </pre> * The output will be the same irrespective of the machine that the code is running on. * * @param fileName the filename to query, null returns null * @return the name of the file without the path, or an empty string if none exists */ public static String getName(String fileName) { if (fileName == null) { return null; } int index = indexOfLastSeparator(fileName); return fileName.substring(index + 1); }