I have a problem. I have some file without extension and I need discover the extension. How can I do that? I think the only way is to read the file, but i don't know how I can understand the extension. Thanks
Asked
Active
Viewed 76 times
-1
-
1Show some code. What you did? – kosa May 28 '15 at 15:06
-
@Arpit: They didn't mean to get the file extension, rather recognize the file format without having the extension – wonderb0lt May 28 '15 at 15:08
-
You could use [Apache Commons IO](http://stackoverflow.com/a/16202288/284538) for this. – Mike Thomsen May 28 '15 at 15:09
-
@Wonderb0lt: Correct!, removed it. Thanks. – Arpit Aggarwal May 28 '15 at 15:10
-
Perhaps you could edit the question to clarify that you want **to check the file type** of a file, not just substring the extension out of the file name. – nuno May 28 '15 at 15:14
4 Answers
1
To probe the content of a File
you could use the nio Java API and the method probeContentType

nuno
- 1,771
- 1
- 19
- 48
0
If your file has no extension (as you said) you cannot read the extension, because it is not present.
You could read the start of the file and look for some indicators of the content. E.g. most .pdf
files start with %PDF-1.5
(or a different version number) in the content.

Simulant
- 19,190
- 8
- 63
- 98
0
I think that the only way is read the magic number of the file...how?
Check this thread Reading the header of a file in java
Hope that help you!

Community
- 1
- 1

appersiano
- 2,670
- 22
- 42
0
Without extension, it's tough to find the format (but it's possible).
Even Windows
asks you What to do when you click on Open With..
on a file without extension.

K139
- 3,654
- 13
- 17