I want to read files header. The reason is that I want to verify that the file is of a specific type mp3, pdf, exe etc
Is there some API that can help me?
Printable View
I want to read files header. The reason is that I want to verify that the file is of a specific type mp3, pdf, exe etc
Is there some API that can help me?
even i want to know abt this..cn ny1 help me out
the class MimetypesFileTypeMap can be what you look for:
javax.activation.MimetypesFileTypeMap
it can tell you the MIME type of a file.... of course the problem is that some files like mp3 or pdf can have the same MIME type --> application/octet-stream
example:
another option....maybe better for you is FileSystemView:Code:File f = new File(filepath);
System.out.println("Mime Type of " + f.getName() + " is " +
new MimetypesFileTypeMap().getContentType(f));
javax.swing.filechooser.FileSystemView
it will tell you the system dependent type description of the file
example:
the problem of this method is that it will give different descriptions for file type, depending on the OS, on the language and on the software which registered for the interested file type:Code:File f = new File(filepath);
System.out.println("System Type description of " + f.getName() + " is " +
FileSystemView.getFileSystemView().getSystemTypeDescription(f));
example:
a pdf file can return "Adobe Acrobat 7.0 Document", "Adobe Acrobat Document", ......
EDIT:
another ....maybe even better option can be found at
schmidt.devlib.org/ffident/index.html
example:
this one doesn't check only the extension but i think it does a check also on the content and verifies it matchesCode:File f = new File(filepath);
FormatDescription desc = FormatIdentification.identify(f);
if (desc == null) {
System.out.println("Unknown format.");
} else
System.out.println("Format=" + desc.getShortName() + ", MIME type=" + desc.getMimeType());
i hope this reply helped
How do i use FormatDescriptor??:confused:
well the FormatDescription class can tell you with it's methods:
getShortName() -> the shor name of the type
example : PDF, JPG, MP3,...
getMimeType() -> a litte more detailed description
with this information you can make your program take decisions depending of the info.
stupid example:
File f = new File(filepath);
FormatDescription desc = FormatIdentification.identify(f);
Code:if (desc.getShortName().equals("PDF")
System.out.println("You opened a PDF");
else if (desc.getShortName().equals("MP3")
System.out.println("You opened an MP3");
thanks wolfcro..cud u tel me any method to get the mp3 file's time...so tht i cn use wait fot tht much time
cn thr be any wy to knw if the file is corrupted or not from the header wy or sum othr
try searching google for :
java read mp3 tag
and you'll find some libraries for reading mp3 info
please help me out...how cn i use these libraries.....PLEASE HELP ME IN MY PROJECT:((
It appears to me that Wolfco is helping you with your project... remember this is voluntary help in this forum. I don't think it's a good idea to post demanding statements (caps & bolded) requesting/demanding help. It might have the oposite effect.
CJSL