简体   繁体   中英

Read/Write from a binary file

import java.io.*;
public class Main2 {
    public static void main(String[] args) throws Exception {
        DataOutputStream dos = new DataOutputStream(new FileOutputStream("Text.t"));
        dos.writeByte(10101010);
        DataInputStream dis = new DataInputStream(new FileInputStream("Text.t"));
        int line;
        System.out.println(dis.readByte());
        dos.close();
        dis.close();
    }
}

i am trying to write 10101010 in a binary file that i create and print its content. When i run this it shows 18.. why? why not 10101010?Moreover when i open the Text.t file with textpad it contains this "rubbish" and not 10101010.

10101010 % 256 = 18. This is the low byte of the integer you created. The text file is filed with "rubbish" because you saved this as binary data not as text.

If you want your number to be saved as a binary string, you should use a FileWriter and a FileReader rather than a DataInput/OutputStream, and use Integer.toBinaryString(int) and Integer.parseInt(str,2);

It's because you're printing binary into it, then reading the binary out. That binary code 10101010 corresponds to the arrow you're seeing, but when it's read back out, it's processed by Java and so comes out as a number, rather than the ASCII (or Unicode, depending on how it's encoded by default) representation.

You are writing a single byte to file Text.t . If you open that file in a text editor then you will see a single-character representation of that byte, according to whatever character encoding the editor decides to use.

If you want to see an eight-digit string of zeroes and ones then you want a text file, not a binary file, and you should output the data as a String .

Moreover, Java does not recognize your number as a binary literal. The value 10101010 is a decimal literal representing a number slightly greater than ten million. It will be truncated to a single byte when passed to dos.writeByte() . If you really want to output the single byte whose binary representation is 10101010 , then you can express it as 0b10101010 , or as a hexadecimal literal:

dos.writeByte(0b10101010);

or

dos.writeByte(0xAA);

Opening a file or other datastream as binary does not mean that your integer operations now all take inputs with values expressed with the binary number system. It simply means that the data you're working with is not data likely to be read by a human.

The writeByte method on DataOutputStream takes an int input and writes it out as a byte. Therefore, your call dos.writeByte(10101010); writes the least significant 8 bits of the decimal integer value 10101010 . This value happens to be 0b00010010 , which is binary for 18. This is where that is coming from.

If you want to express the binary value 0b10101010 in your file, you need to write the integer value 170: dos.writeByte(170) or dos.writeByte(0xaa) .

Your call to readByte suffers from the same problem. It reads a byte and System.out.println will output whatever character expresses that byte. If you want to print out the binary value of a byte, see this other SO question .

Since your are calling writeByte() on a decimal integer you are getting unexpected results. WriteByte() writes an integer in the space of 1-byte, or 8 bits. The decimal number 10101010 is the same as the binary number 1001 1010 0010 0001 0001 0010 whose final 8 bits are 0001 0010 which is 18 as a decimal number.

To fix this, have java interpret your 10101010 as a binary number with 0b prefixing it to turn it into a binary literal .

dos.writeByte(0b10101010); 

Then to read this data back as a decimal string, you could use

Byte b = (dis.readByte());
String decimalString = String.format("%8s", Integer.toBinaryString(b1 & 0xFF)).replace(' ', '0');
System.out.println(decimalString);

The string conversion courtesy of João Silva for this question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM