-1

I have a binary file that I need to read and save as characters or a string of 0's and 1's in the same order that they are in the binary file. I am currently able to read in the binary file, but am unable to obtain the 0's and 1's. Here is the code I am currently using:

public void read()
{
    try
    {
        byte[] buffer = new byte[(int)infile.length()];
        FileInputStream inputStream = new FileInputStream(infile);

        int total = 0;
        int nRead = 0;
        while((nRead = inputStream.read(buffer)) != -1)
        {
            System.out.println(new String(buffer));
            total += nRead;
        }
        inputStream.close();
        System.out.println(total);
    }
    catch(FileNotFoundException ex)
    {
        System.out.println("File not found.");
    }

    catch(IOException ex)
    {
        System.out.println(ex);
    }
}  

and the output from running this with the binary file:

 �, �¨Ã �¨Êà   
�!Cˇ¯åaÃ!Dˇ¸åÇÃ�"( ≠EÃ!J�H���û�������  
����������������������������������������������������������������������������������������  
156

Thanks for any help you can give.

Avi
  • 21,182
  • 26
  • 82
  • 121
user2800796
  • 1
  • 1
  • 1

4 Answers4

0

Check out String to binary output in Java. Basically you need to take your String, convert it to a byte array, and print out each byte as a binary string.

Community
  • 1
  • 1
Blake
  • 2,047
  • 2
  • 12
  • 11
0

Instead of converting the bytes directly into characters and then printing them, convert each byte into a binary string and print them out. In other words, replace

System.out.println(new String(buffer));

with

for (int i = 0; i<nRead; i++) {
    String bin=Integer.toBinaryString(0xFF & buffer[i] | 0x100).substring(1);
    System.out.println(bin);
}

Notice though that the bits of each byte are printed in big-endian order. There is no way to know if bits are actually stored in this order on disk.

Joni
  • 108,737
  • 14
  • 143
  • 193
0

with JBBP such operation will be very easy

public static final void main(final String ... args) throws Exception {
    try (InputStream inStream = ClassLoader.getSystemClassLoader().getResourceAsStream("somefile.txt")) {
      class Bits { @Bin(type = BinType.BIT_ARRAY) byte [] bits; }
      for(final byte b : JBBPParser.prepare("bit [_] bits;",JBBPBitOrder.MSB0).parse(inStream).mapTo(Bits.class).bits)
        System.out.print(b != 0 ? "1" : "0");
    }
  }

But it will not be working with huge files because parsed data will be cached in memory during operatio

Igor Maznitsa
  • 833
  • 7
  • 12
-1

Even though this response is in C, you can use the JNI to access it natively from a Java program.

Since they are in a binary format, you will not be able to read it. I would do it like this.

fstream fs;
int value; //Since you are reading bytes, change accordingly.
fs.open( fileName, is.in | is.binary );

fs.read((char *) &value, sizeof(int));

while(!fs.eof())
{
    //Print or do something with value
    fs.read((char *) &value, sizeof(long));
}           
Community
  • 1
  • 1
Shashank
  • 325
  • 4
  • 12