• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to determine a file type  RSS feed

 
n Cooler
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
n Cooler
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, thank you. It looks like this is not a trivial task. Thanks again.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
n Cooler
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks like exactly what I need. I'll try it out and let you know. THanks.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!