5

Does any one know how to read the header of a file in java using "magic numbers" or ascii values to get the name of the extension of a file

9
  • do you mean the file name by "header of a file"?
    – talnicolas
    Nov 19, 2011 at 3:45
  • 2
    He means the first few bytes, specifically in an image or media file. Technically you probably could consider it a "header" I suppose. Judging by his previous question he doesn't seem to understand that the "extension" is part of the filename. Nov 19, 2011 at 3:46
  • @BrianRoach I am a girl...and u r right I should identify the file type by readin the first few bytes like a header. I dont understand it much so al the help is appreciated
    – sue yin
    Nov 19, 2011 at 4:03
  • @talnicolas I should identify the first few bytes of a file by reading its header in order to know the file extension
    – sue yin
    Nov 19, 2011 at 4:05
  • @sue yin, the general solution to the problem you are describing is not at all simple. Do you have some specific file types you want to distinguish between?
    – clstrfsck
    Nov 19, 2011 at 4:18

2 Answers 2

4

Maybe not the answer you wanted, but as you gave us very little information ...

In unixoid systems (Linux, Mac, *BSD) you have the file command, that

tests each argument in an attempt to classify it. There are three sets of tests, performed in this order: filesystem tests, magic tests, and language tests. The first test that succeeds causes the file type to be printed.

E.g.

$ file linux-image-3.1.0-030100rc10-generic_3.1.0-030100rc10.201110200610_amd64.deb
linux-image-3.1.0-030100rc10-generic_3.1.0-030100rc10.201110200610_amd64.deb: Debian binary package (format 2.0)

Using Runtime.exec(...) you could invoke that program and parse its output.

Edit 1:

To determine if a given file is a PNG:

import java.io.*;

public class IsPng {

    public static void main(String ...filenames) throws Exception {
        if(filenames.length == 0) {
            System.err.println("Please supply filenames.");
            return;
        }

        for(String filename : filenames) {
            if(isPng(new File(filename))) {
                System.out.println(filename + " is a png.");
            } else {
                System.out.println(filename + " is _not_ a png.");
            }
        }
    }

    private static final int MAGIC[] = new int[] { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };

    private static boolean isPng(File filename) throws Exception {
        FileInputStream ins = new FileInputStream(filename);
        try {
            for(int i = 0; i < MAGIC.length; ++i) {
                if(ins.read() != MAGIC[i]) {
                    return false;
                }
            }
            return true;
        } finally {
            ins.close();
        }
    }

}

Edit 2:

Sometimes URLConnection.getContentType() works, too, for local files:

new File(name).toURI().toURL().openConnection().getContentType()

But your comments sound like you have to implement the method by yourself, not using external programs (?).

2
  • I do not understand wat u jus typed..I dont think that code is what am lookin for.. I think it is a bit simpler tahn that...at least i hope...thanks still
    – sue yin
    Nov 19, 2011 at 4:04
  • I added an example which determines if a given file is a .png. You would need to test many magics, though, if you want to support multiple filetypes.
    – Kijewski
    Nov 19, 2011 at 4:15
2

You can try JFileChooser. Here is an example.

import java.io.*;
import javax.swing.*;

class GetFileType {
  public static void main(String[] args){
    JFileChooser chooser = new JFileChooser();
    File file = new File("Hello.txt");

    String fileTypeName = chooser.getTypeDescription(file);
    System.out.println("File Type= "+fileTypeName);
  }
}

This will output Text Document. If it is a MP3 file passed then the output will be MP3 Format Sound.

Not the answer you're looking for? Browse other questions tagged or ask your own question.