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

Kijewski
  • 25,517
  • 12
  • 101
  • 143
sue yin
  • 91
  • 1
  • 1
  • 5
  • do you mean the file name by "header of a file"? – talnicolas Nov 19 '11 at 03: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. – Brian Roach Nov 19 '11 at 03: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 '11 at 04: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 '11 at 04: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 '11 at 04:18
  • @sue yin: While the start of a file can be used fairly reliably for determining a [file type](http://en.wikipedia.org/wiki/File_type), on every system with which I am familiar, the file extension cannot be definitively determined from [magic numbers](http://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) at the start of a file. For example nothing prevents you from putting a .txt extension on an executable file. – PTBNL Nov 19 '11 at 06:36
  • @PTBNL Yeah ...even if u change the name of the extension it should still be able to tell u the correct extension o fthe file by reading the header of that file... – sue yin Nov 19 '11 at 12:49
  • @msandiford kinda..like pdf, mp3, docx, mp4, mov, jpeg, png, gif – sue yin Nov 19 '11 at 12:49
  • @talnicolas: https://en.wikipedia.org/wiki/File_format#File_header – hippietrail May 16 '20 at 00:49

2 Answers2

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 (?).

Kijewski
  • 25,517
  • 12
  • 101
  • 143
  • 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 '11 at 04:04
  • I added an example which determines if a given file is a [.png](http://en.wikipedia.org/wiki/Portable_Network_Graphics). You would need to test many magics, though, if you want to support multiple filetypes. – Kijewski Nov 19 '11 at 04: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.

One-One
  • 321
  • 1
  • 15
  • 32