Results 1 to 10 of 10
  1. #1
    mew
    mew is offline Member
    Join Date
    Nov 2007
    Posts
    70
    Rep Power
    0

    Default Reading file header

    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?

  2. #2
    pranjal is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    even i want to know abt this..cn ny1 help me out

  3. #3
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Default File type

    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:
    Java Code:
    File f = new File(filepath);
    System.out.println("Mime Type of " + f.getName() + " is " +  
          new MimetypesFileTypeMap().getContentType(f));
    another option....maybe better for you is FileSystemView:
    javax.swing.filechooser.FileSystemView

    it will tell you the system dependent type description of the file

    example:
    Java Code:
    File f = new File(filepath);
    System.out.println("System Type description of " + f.getName() + " is " +  
          FileSystemView.getFileSystemView().getSystemTypeDescription(f));
    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:

    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:
    Java Code:
    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());
    this one doesn't check only the extension but i think it does a check also on the content and verifies it matches

    i hope this reply helped
    Last edited by wolfcro; 02-10-2009 at 01:43 PM.

  4. #4
    pranjal is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    How do i use FormatDescriptor??:confused:

  5. #5
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Default

    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);

    Java 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");

  6. #6
    pranjal is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    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

  7. #7
    pranjal is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    cn thr be any wy to knw if the file is corrupted or not from the header wy or sum othr

  8. #8
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Default

    try searching google for :
    java read mp3 tag

    and you'll find some libraries for reading mp3 info

  9. #9
    pranjal is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    please help me out...how cn i use these libraries.....PLEASE HELP ME IN MY PROJECT:((
    Last edited by pranjal; 02-21-2009 at 07:35 AM.

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Not good...

    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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Reading a properties file
    By peiceonly in forum New To Java
    Replies: 7
    Last Post: 05-06-2010, 06:17 PM
  2. Servlet sending the file (setting header)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-27-2008, 09:14 PM
  3. Reading a file
    By mew in forum New To Java
    Replies: 2
    Last Post: 12-30-2007, 01:23 PM
  4. Reading a file for use
    By peachyco in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 04:49 AM
  5. Reading from a file
    By leebee in forum New To Java
    Replies: 1
    Last Post: 07-23-2007, 01:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •