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
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.
close() method closes this file input stream and releases any system resources associated with the stream.
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.
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.
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.
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();
}
}
}
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 (?).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With