Tests to see if the filename has a any of the standard file extensions of common image formats - Java 2D Graphics

Java examples for 2D Graphics:Image File

Description

Tests to see if the filename has a any of the standard file extensions of common image formats

Demo Code


//package com.java2s;

public class Main {
    /**/*from  ww  w . j a  va 2 s.co  m*/
     * Tests to see if the filename has a any of the standard file extensions of
     * common image formats
     * 
     * @param filename
     *            must have a length of at least 4 due to this method looking or
     *            a '.' before the file extentsion
     * @return true if the sting contains any of the standard file extensions
     *         for JPEG, GIF, PNG, and Bitmap image formats
     */
    public static boolean isImageFile(String filename) {
        boolean isImage = false;
        if (!(filename.length() > 4)) {
            return false;
        }

        int i = filename.length();
        String ending = filename.substring(i - 4, i);

        if (ending.equalsIgnoreCase(".gif")) {
            isImage = true;
        } else if (ending.equalsIgnoreCase(".bmp")) {
            isImage = true;
        } else if (ending.equalsIgnoreCase(".jpg")) {
            isImage = true;
        } else if (ending.equalsIgnoreCase(".png")) {
            isImage = true;
        }

        // the ending needs a new call to substring for the files with 4 digit
        // extensions
        ending = filename.substring(i - 5, i);
        if (ending.equalsIgnoreCase(".jpeg")) {
            isImage = true;
        }

        return isImage;
    }
}

Related Tutorials