Home >Java >javaTutorial >Reading and writing binary data

Reading and writing binary data

Linda Hamilton
Linda HamiltonOriginal
2024-11-05 08:13:01863browse
  • 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.

Lendo e gravando dados binários

Lendo e gravando dados binários

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

Lendo e gravando dados binários

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn