Here you can find the source of getFileExtension(String filename)
public static String getFileExtension(String filename)
//package com.java2s; //License from project: Apache License import java.io.File; public class Main { public static String getFileExtension(String filename) { // returns extension including . // Now strip of possible extension. String extension = ""; int index = filename.lastIndexOf('.'); if (index != -1) extension = filename.substring(index); return extension; }/*from w w w .ja v a 2 s. c o m*/ public static String getFileExtension(File file) { // returns extension including . // Strip of path first. String base = file.getName(); // Now strip of possible extension. String extension = ""; int index = base.lastIndexOf('.'); if (index != -1) extension = base.substring(index); return extension; } public static String getFileExtension(String filename, boolean keepDot) { filename = filename.replace('\\', '/'); String name; int namePos = filename.lastIndexOf('/'); if (namePos != -1) { name = filename.substring(namePos + 1); } else { // no path info found name = filename; } String ext; int extPos = name.lastIndexOf('.'); if (extPos != -1) { if (keepDot) ext = name.substring(extPos); else ext = name.substring(extPos + 1); } else { // no extension found ext = ""; } return ext; } public static String getFileExtension(File file, boolean keepDot) { // Strip path first. String base = file.getName(); String extension = ""; int index = base.lastIndexOf('.'); if (index != -1) { if (keepDot) extension = base.substring(index); else extension = base.substring(index + 1); } return extension; } }