Java File getFileExtension() Utility Method

Java File getFileExtension() Utility Method - Get file extension such as "txt","png","pdf".

Java File getFileExtension() Utility Method - Get file extension such as "txt","png","pdf"

/**
 *  Get file extension such as "txt","png","pdf"
 * @param file
 * @return
 */
public static String getFileExtension(File file) {
    String fileName = file.getName();
    if (fileName.lastIndexOf('.') != -1 && fileName.lastIndexOf('.') != 0) {
        return fileName.substring(fileName.lastIndexOf('.') + 1);
    } else {
        return "File don't have extension";
    }
}



Comments