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?Answer1:
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 <a href="https://stackoverflow.com/questions/51438/getting-a-files-mime-type-in-java" rel="nofollow">that question</a> contain pointers to some such libraries.Answer2:
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 <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?file" rel="nofollow">
file</a> utility. The version that's installed on my Ubuntu box lists Microsoft Word among the "magic" signatures that it knows about.
What you want to do is find out what its MIME type is. You can use this tutorial <a href="http://www.roseindia.net/java/java-get-example/get-mime-type.shtml" rel="nofollow">http://www.roseindia.net/java/java-get-example/get-mime-type.shtml</a>Answer4:
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 undoAnswer5:
javax.activation.FileDataSource from the JavaBeans Activation Framework. In particular use the <a href="http://docs.oracle.com/cd/E17802_01/j2se/javase/technologies/desktop/javabeans/glasgow/javadocs/javax/activation/FileDataSource.html#getContentType%28%29" rel="nofollow" title="getContentType()">getContentType()</a> method.