3

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?

user896015
  • 31
  • 1
  • 3

5 Answers5

1

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.

Community
  • 1
  • 1
Romain
  • 12,679
  • 3
  • 41
  • 54
  • See this question how to examine files by content: http://stackoverflow.com/questions/51438/getting-a-files-mime-type-in-java – Aaron Digulla May 09 '12 at 12:47
1

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.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

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

A123321
  • 837
  • 11
  • 21
0

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

iosMentalist
  • 3,066
  • 1
  • 30
  • 40
0

Consider using javax.activation.FileDataSource from the JavaBeans Activation Framework. In particular use the getContentType() method.

alexg
  • 3,015
  • 3
  • 23
  • 36