-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.

1
  • Is there a particular reason you need the 1s and 0s, or do you just need to copy this file somewhere else? Commented Sep 20, 2013 at 22:02

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

-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));
}           

1 Comment

Doesn't answer the question in any way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.