Reputation: 31
How to find file extension if the file has been renamed? Is there any tool are available for this?
Example: I have a file "1.doc"; I hope everybody know this is a Word document I just renamed as "1.txt". But the file is a Word document originally; how can I get the original file extension?
Upvotes: 3
Views: 3935
Reputation: 3055
Consider using javax.activation.FileDataSource
from the JavaBeans Activation Framework. In particular use the getContentType() method.
Upvotes: 0
Reputation: 500465
Once you rename the file, the previous name is not preserved.
As far as automatically guessing the format of the file from its contents, take a look at the Unix file
utility. The version that's installed on my Ubuntu box lists Microsoft Word among the "magic" signatures that it knows about.
Upvotes: 1
Reputation: 3084
before renaming it save it in a separate string called originalName...because when you change the name of the string, the memory allocated is changed and theres is no go back or undo
Upvotes: 0
Reputation: 12819
You cannot get the previous file extension back. All you can do is look at the contents of the file, and try to guess what it is.
Most (many?) file formats begin with a "magic" value that is intended to identify the file format, but this method can never be 100% accurate.
As @AaronDigulla mentionned in a comment, there are libraries out there that help you in the task of "guessing", and the answer(s) to that question contain pointers to some such libraries.
Upvotes: 1
Reputation: 837
What you want to do is find out what its MIME type is. You can use this tutorial http://www.roseindia.net/java/java-get-example/get-mime-type.shtml
Upvotes: 0