The flexibility of this language is just outstanding. In it’s own organized way, you can read binary files and get the hex code or even the binary code. Then you can also write your data back to a binary file. Here is how…

To read in binary, you need to use the FileInputStream, or any equivalent that has a read method returning an int. Then, you can convert the read data into hex or binary with the Integer’s methods toBinaryString and toHexString. Here is a small example:

import java.io.FileInputStream;
....
private void printHexFile(String filename){
    FileInputStream in = new FileInputStream(filename);
    int read;
    while((read = in.read()) != -1){
        System.out.print(Integer.toHexString(read) + "\t");
    }
}
private void printBinaryFile(String filename){
    FileInputStream in = new FileInputStream(filename);
    int read;
    while((read = in.read()) != -1){
        System.out.print(Integer.toBinaryString(read) + "\t");
    }
}

This way you can get the data of any file and print it in hex or binary. If you want to change some data in hex or binary mode and want to store them into a file you have to convert your hex/binary back into int and then save them with a simple write that gets as an argument an int such as the one that the FileOutputStream has. To convert your hex/binary to int you can use the parseInt method like this:

//convert the String to int which has a base two.
int num1 = Integer.parseInt("01001100", 2);
//convert the String to int that has a base of 16
int num2 = Integer.parseInt("FF", 16);

This way you can read data from a file in hex or binary, make any changes you want and then save them back to the file. Ofcourse, you can use bitwise operators such as & etc if it suits you. This way it is slower but it is more easily to understand.