We can read and write data beyond ASCII characters, including primitive types such as int, double and short.
DataInputStream and DataOutputStream are used to manipulate binary data of primitive types in files.
DataOutputStream implements the DataOutput interface, which contains methods for recording all Java primitive types.
Data is written in internal binary format, not in textual format.
DataOutputStream methods throw an IOException in case of error.
The DataOutputStream constructor requires an OutputStream object as a parameter to specify the output stream.
FileOutputStream can be used to create the output stream to which data will be written.
DataInputStream implements the DataInput interface for reading all Java primitive types.
All read methods can throw an IOException.
The class uses an instance of InputStream as a base, adding methods to read Java data types.
DataInputStream reads data in binary, non-human readable format.
Constructor: DataInputStream(InputStream flowInput), where flowInput defines the input stream.
For reading files, FileInputStream can be used as InputStream parameter.
Usage example: program that writes and reads different types of data to a file using DataOutputStream and DataInputStream.
The program output is shown here.
Writing 10
Writing 1023.56
Writing true
Writing 90.28
Reading 10
Reading 1023.56
Reading true
Reading 90.28
The above is the detailed content of Reading and writing binary data. For more information, please follow other related articles on the PHP Chinese website!