79986

How to find file extension if the file has been renamed?

Question:

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.

Answer3:

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 undo

Answer5:

Consider using 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.

Recommend

  • How to remove or strip off white spaces without using strip() function?
  • What happened between March 28th and March 29th, 1976 with the java.util.GregorianCalendar?
  • How do you make Git work with IntelliJ?
  • Modifying list view in Sonata
  • Get Weekday name from index number in MYSQL
  • Swift - pruning elements from an Array, converting integer strings to integers
  • Can't get any output from the taglib
  • Add class element based on the name of the page
  • How to transform Byte[](decoded as PNG or JPG) to Tensorflows Tensor
  • HALF_PTR Windows data type
  • Is it safe to accept URL parameters for populating the `url_for` method?
  • Implementation of RTTI using typeid
  • include dlls in visual studio c++ 2008
  • Javascript focus remove text highlight
  • Why can't UI components be accessed from a backgroundworker?
  • react split panel resize
  • Can you pass an array from javascript to asp.net mvc controller action without using a form?
  • In C what exactly happens if i use () to initialize a double dimension array instead of the {}?
  • How to explicitly/implicitly implemented interface members in C++/CLI?
  • Symfony 2. CSRF token is invalid
  • c# open webrowser in many tab
  • Z3: Convert between FP and BitVector?
  • Textfile Structure (tables)
  • chrome.tabs.executeScript only fires when the Developer Console is open
  • Custom Tabgroup Appcelerator
  • $wpdb not working in file of WordPress plugin
  • Recording logins for password protected directories
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Trying to switch camera back to front but getting exception
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • jQuery tmpl and DataLink beta
  • Arrays break string types in Julia
  • SQL merge duplicate rows and join values that are different
  • WPF Applying a trigger on binding failure
  • Proper way to use connect-multiparty with express.js?
  • Free memory of cv::Mat loaded using FileStorage API
  • How to set the response of a form post action to a iframe source?
  • Angular 2 constructor injection vs direct access
  • Programmatically clearing map cache