Here you can find the source of fileExtension(CharSequence path)
Parameter | Description |
---|---|
path | A file withname such as /a/b/foo.html ; the file need not actually exist. |
public static String fileExtension(CharSequence path)
//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; } }