my dog learned polymorphism
The moose likes Java in General and the fly likes How to determine a file type Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to determine a file type" Watch "How to determine a file type" New topic
Author

How to determine a file type

n Cooler
Greenhorn

Joined: Sep 05, 2007
Posts: 5
I need to determine the file type for a list of files in my Java program (Java 1.4). Can anyone tell me how to do this? Much appreciated.
Arthur Buliva
Ranch Hand

Joined: Mar 08, 2006
Posts: 101
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Google for mime types. There are tables of hundreds of extensions with descriptive types and some hints about what programs might open them. This kind of thing isn't perfect ... some extensions are ambiguous and might have several meanings.

Some files have a little "signature" at the beginning, like Java class files all start with x'CAFEBABE. I don't know a good catalog of these, but there aren't many compared to the number of file types in the world and they would be easy to fake if somebody wanted to confuse you.

Is that the kind of thing you're after?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
n Cooler
Greenhorn

Joined: Sep 05, 2007
Posts: 5
Yes, thank you. It looks like this is not a trivial task. Thanks again.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12929
    
    3

Maybe the JavaBeans Activation Framework does what you need:
JavaBeans Activation Framework (JAF) is a standard extension to the Java platform that lets you take advantage of standard services to: determine the type of an arbitrary piece of data; encapsulate access to it; discover the operations available on it; and instantiate the appropriate bean to perform the operation(s).

I haven't tried it out, but you should be able to use that to detect the MIME type of a file like this:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
n Cooler
Greenhorn

Joined: Sep 05, 2007
Posts: 5
That looks like exactly what I need. I'll try it out and let you know. THanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to determine a file type
 
Similar Threads
How to get File extension?
File type in Mac
int
How to make sure a file is a valid text file.
How Writing information to a database