Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading the header of a file in java [closed]

Tags:

java

file-type

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

like image 713
sue yin Avatar asked Nov 19 '11 03:11

sue yin


People also ask

How do I read a header file?

You "#include" the header when you write your code (b); then the compiler reads the header when it compiles the program (c). The header is only important in steps b) and c). The only thing you do is "#include" the header in your source code. The compiler does the rest.

What happens when you close a file in Java?

close() method closes this file input stream and releases any system resources associated with the stream.

How do I open a Java reading file?

Java FileInputStream class is used to open and read a file. We can open and read a file by using the constructor of the FileInputStream class. The signature of the constructor is: public FileInputStream(File file) throws FileNotFoundException.

Why header file is not used in Java?

You don't need header files because the Java compiler compiles class definitions into a binary form that retains all the type information through to link time. By removing all this baggage, Java becomes remarkably context-free.


2 Answers

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

like image 105
kay Avatar answered Nov 05 '22 11:11

kay


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.

like image 2
One-One Avatar answered Nov 05 '22 09:11

One-One