简体   繁体   English

从二进制文件读取/写入

[英]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. 我试图在我创建并打印其内容的二进制文件中写入10101010。 When i run this it shows 18.. why? 当我运行它时,它显示18 ..为什么? why not 10101010?Moreover when i open the Text.t file with textpad it contains this "rubbish" and not 10101010. 为什么不10101010?此外,当我使用文本板打开Text.t文件时,它包含此“垃圾”而不是10101010。

10101010 % 256 = 18. This is the low byte of the integer you created. 10101010%256 =18。这是您创建的整数的低字节。 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); 如果要将数字保存为二进制字符串,则应使用FileWriterFileReader而不是DataInput / OutputStream,并使用Integer.toBinaryString(int)和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. 二进制代码10101010对应于您所看到的箭头,但是当它被读出时,它是由Java处理的,因此以数字而不是ASCII(或Unicode,取决于默认编码方式)表示。

You are writing a single byte to file Text.t . 您正在向文件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 . 如果要查看零和八位数字的八位数字符串,则需要文本文件而不是二进制文件,并且应将数据输出为String

Moreover, Java does not recognize your number as a binary literal. 而且,Java无法将您的数字识别为二进制文字。 The value 10101010 is a decimal literal representing a number slightly greater than ten million. 10101010十进制文字,表示稍大于一千万的数字。 It will be truncated to a single byte when passed to dos.writeByte() . 传递给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: 如果您确实要输出二进制表示为10101010的单个字节,则可以将其表示为0b10101010或十六进制文字:

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. DataOutputStream上的writeByte方法采用int输入并将其写为字节。 Therefore, your call dos.writeByte(10101010); 因此,您的呼叫dos.writeByte(10101010); writes the least significant 8 bits of the decimal integer value 10101010 . 写入十进制整数值10101010的最低有效8位。 This value happens to be 0b00010010 , which is binary for 18. This is where that is coming from. 该值恰好是0b00010010 ,它是18的二进制值。

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) . 如果要在文件中表示二进制值0b10101010 ,则需要写入整数值170: dos.writeByte(170)dos.writeByte(0xaa)

Your call to readByte suffers from the same problem. 您对readByte调用也遇到了同样的问题。 It reads a byte and System.out.println will output whatever character expresses that byte. 它读取一个字节, System.out.println将输出表示该字节的任何字符。 If you want to print out the binary value of a byte, see this other SO question . 如果要打印出字节的二进制值,请参阅此其他SO问题

Since your are calling writeByte() on a decimal integer you are getting unexpected results. 由于您是在十进制整数上调用writeByte() ,因此会得到意外的结果。 WriteByte() writes an integer in the space of 1-byte, or 8 bits. WriteByte()在1字节或8位的空间中写入整数。 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. 十进制数10101010与二进制数1001 1010 0010 0001 0001 0010 ,其后8位为0001 0010 ,即十进制数18。

To fix this, have java interpret your 10101010 as a binary number with 0b prefixing it to turn it into a binary literal . 要解决此问题,请让Java将您的10101010解释为一个二进制数字,并以0b作为前缀,以将其转换为二进制文字

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 JoãoSilva为这个问题提供的字符串转换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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