File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Is indexof only way of identify in file type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Is indexof only way of identify in file type" Watch "Is indexof only way of identify in file type" New topic
Author

Is indexof only way of identify in file type

Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 410
Dear All
While making a fileinputstream in case we have to check for file type for enforcing validation rule. Is there any better way except doing an indexOf on File complete path.


SCJP,SCWCD,SCBCD<br />If Opportunity doesn't knock then build the door
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I would use the endsWith() method instead, to avoid the possibility that a file extension might also occur elsewhere in the path.


"I'm not back." - Bill Harding, Twister
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Originally posted by Jim Yingst:
I would use the endsWith() method instead, to avoid the possibility that a file extension might also occur elsewhere in the path.


I would do a getName() on file object and then endsWith()


apigee, a better way to API!
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
If you are wanting to check whether a given file is of the right content type, you may want to look at URLConnection.guessContentTypeFromName() and URLConnection.guessContentTypeFromStream().

Note you can make a URL from a File.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 410
you may want to look at URLConnection.guessContentTypeFromName() and URLConnection.guessContentTypeFromStream().

It seems to be a good solution but is it possible to get URLConnection somehow from request object as I must be doing this check in struts kind of framework where we only have request object to get most of the input
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
URLConnection.guessContentTypeFromName() and -Stream() are static methods, so you don't need an instance. You should be able to call these anywhere.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Yes, you can call them without an instance of URLConnection. My code does it: -

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is indexof only way of identify in file type
 
Similar Threads
Searching and extracting
Detecting 2 periods in 1 String
Vectors
Grabbing char from strings
comparing characters