Exercise
Reading a binary file (1: BMP)
Objetive
Create a java program to check if a BMP image file seems to be correct.
It must see if the first two bytes are B and M (ASCII codes 0x42 and 0x4D).
Example Code
public class Main
{
public static void main(String[] args)
{
byte data1, data2;
//Open file
BinaryReader myFile;
myFile = new BinaryReader(File.Open("1.bmp", FileMode.Open));
// Read data
data1 = myFile.ReadByte();
data2 = myFile.ReadByte();
//Close file
myFile.Close();
//Check Data
if ((data1 == 0x42) && (data2 == 0x4D)) // B M
{
System.out.println("It seems to be a BMP file");
}
else
{
System.out.println("It DOES NOT seem to be a BMP file");
}
}
}