Java File Extension Name Get fileExtension(CharSequence path)

Here you can find the source of fileExtension(CharSequence path)

Description

Given a file name, this method checks to see if path ends with an extension such as ".java", ".yml", etc.

License

Open Source License

Parameter

Parameter Description
path A file withname such as /a/b/foo.html ; the file need not actually exist.

Return

If path ends with an extension it is returned (including the . ). If there is no extension null is returned.

Declaration

public static String fileExtension(CharSequence path) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   w ww .j  a  va 2 s. co m*/
     * Given a file name, this method checks to see if {@code path} ends
     * with an extension such as ".java", ".yml", etc.  If so the
     * extension is returned.
     * @param path                    A file withname such as {@code /a/b/foo.html};
     *                                the file need not actually exist.
     * @return                        If {@code path} ends with an
     *                                extension it is returned (including the
     *                                {@code .}).  If there is no extension
     *                                null is returned.
     */
    public static String fileExtension(CharSequence path) {
        int index = findExtension(path);
        if (index >= 0) {
            return path.subSequence(index, path.length()).toString();
        }
        return null;
    }

    /**
     * Given a file name, this method checks to see if {@code path} ends
     * with an extension such as ".java", ".yml", etc.
     * @param path                    A file name such as {@code /a/b/foo.html};
     *                                the file need not actually exist.
     * @return                        If {@code path} ends with an
     *                                extension the return value is the index
     *                                in {@code path} of the extension's
     *                                initial ".".  Otherwise the return
     *                                value is -1.
     */
    public static int findExtension(CharSequence path) {
        // Work backwards from the end of the string to see if we find a
        // "." before a "/".

        int length = path.length();
        for (int i = length - 1; i >= 0; i--) {
            char c = path.charAt(i);
            if (c == '.') {
                if (i == (length - 1)) {
                    // There is a "dot", but there is nothing after it.
                    return -1;
                }
                return i;
            }
            if ((c == '/') || (c == '\\')) {
                return -1;
            }
        }
        return -1;
    }
}

Related

  1. extractExtension(String strFilename)
  2. extractExtension(String url)
  3. extractExtensions(String fileName)
  4. fileExt(String name)
  5. fileExt(String url)
  6. fileExtension(String filename)
  7. fileExtension(String path)
  8. getFileExtension(File _file)
  9. getFileExtension(File f)