-4

How to find the type of the file? I am writing a code to find that file on unix system is image file or not?

Possibilities exists that someone has changed the extension.

Thanks in advance.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Vishrut Shah
  • 41
  • 1
  • 4
  • Maybe a duplicate of this? http://stackoverflow.com/questions/1915317/howto-extract-mimetype-from-a-byte – Martin Atkins Mar 17 '13 at 05:59
  • `ImageIO.read(..)` is a good way to test if a chunk of bytes represents an image (as understood by J2SE). – Andrew Thompson Mar 17 '13 at 06:03
  • @Martin, unlikely, that's for a mime type, which is generally a file being downloaded. MIME types aren't usually stored alongside arbitrary files on UNIX filesystems. – paxdiablo Mar 17 '13 at 06:03
  • Well, MIME types are one way to describe the type of a file that happens to be pretty standard. Ultimately that question is about using magic numbers at the beginning of a file to determine file type, which is really the only way to heuristically detect file type from raw file contents with no additional context. – Martin Atkins Mar 17 '13 at 17:46

2 Answers2

1

Use the file command:

file <name of file>
erlandsson
  • 434
  • 3
  • 7
0

Java 7 has Files.probeContentType(Path path) which probes the content type of a file. See API for details.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275