how to identify gif file

i want to check if a file is .gif file and if so, do some action. how to do this using file io package
[109 byte] By [ArpanaKa] at [2007-10-3 4:39:06]
# 1

> i want to check if a file is .gif file and if so, do

> some action. how to do this using file io package

Easy:

if (thefile.getName().toLowerCase().endsWith(".gif")) ...

Complicated:

look up the GIF specs for the file header definitions and look whether those in your file match.

CeciNEstPasUnProgrammeura at 2007-7-14 22:43:01 > top of Java-index,Java Essentials,Java Programming...
# 2
Elaborating the complicated one: A GIF has a signature as 'GIF87a' or 'GIF 89a' for the first 6 bytes. Read the first 6 bytes of the file and check for the signature.
aniseeda at 2007-7-14 22:43:01 > top of Java-index,Java Essentials,Java Programming...
# 3
thanks for the replies. i will try that..
ArpanaKa at 2007-7-14 22:43:01 > top of Java-index,Java Essentials,Java Programming...
# 4

I dimly recall seeing something in javax.imageio that has heuristics to check file type based on the first few bytes of a file. (Basically the logic already described.) So maybe this logic is already available in the API.

On the other hand my memory could be entirely wrong. Check the docs to find out one way or the other.

paulcwa at 2007-7-14 22:43:01 > top of Java-index,Java Essentials,Java Programming...
# 5

> I dimly recall seeing something in javax.imageio that

> has heuristics to check file type based on the first

> few bytes of a file. (Basically the logic already

> described.) So maybe this logic is already available

> in the API.

The easiest way would be to simply load the image, if the OP needs to use the images instead of just to handle te file. I'm just not sure whether ImageIO supports GIF. It didn't use to, IIRC.

CeciNEstPasUnProgrammeura at 2007-7-14 22:43:01 > top of Java-index,Java Essentials,Java Programming...
# 6
According to the package description, it supports reading, but not writing, of GIF. Although I believe this should change as the Patent on the encryption format for GIF ran out in 2004 if I remember right.
masijade.a at 2007-7-14 22:43:01 > top of Java-index,Java Essentials,Java Programming...
# 7
thanks for the replies , i am working on it...
ArpanaKa at 2007-7-14 22:43:01 > top of Java-index,Java Essentials,Java Programming...