Home  >  Article  >  Java  >  How do Java I/O streams handle binary data?

How do Java I/O streams handle binary data?

王林
王林Original
2024-04-13 14:03:01469browse

Java I/O stream methods for processing binary data include: FileInputStream and FileOutputStream: used to read and write binary files. DataInputStream and DataOutputStream: for advanced reading and writing of primitive data types and strings. Practical case example: reading and writing image files.

Java I/O流如何处理二进制数据?

How Java I/O streams process binary data

Introduction
Java I/O Streams provide a mechanism for processing binary data and are crucial in file reading and writing, network communication, and data processing. This article will introduce the stream types used for processing binary data in Java, including FileInputStream, FileOutputStream, DataInputStream, and DataOutputStream.

FileInputStream and FileOutputStream
These two streams are used to read and write binary files.

  • FileInputStream: Used to read binary data from a file.
  • FileOutputStream: Used to write binary data to a file.

Code example:

// 从文件读取二进制数据
try (FileInputStream fis = new FileInputStream("data.bin")) {
    int data;
    while ((data = fis.read()) != -1) {
        System.out.println(data);
    }
}

// 向文件写入二进制数据
try (FileOutputStream fos = new FileOutputStream("data.bin")) {
    fos.write(1);
    fos.write(2);
    fos.write(3);
}

DataInputStream and DataOutputStream
These two streams allow you to read in a more advanced way Write binary data, allowing you to read and write primitive data types and strings.

  • DataInputStream: Used to read primitive data types from the input stream.
  • DataOutputStream: Used to write primitive data types to the output stream.

Code example:

// 从输入流中读取原始数据类型
try (DataInputStream dis = new DataInputStream(new FileInputStream("data.bin"))) {
    int i = dis.readInt();
    double d = dis.readDouble();
}

// 向输出流中写入原始数据类型
try (DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.bin"))) {
    dos.writeInt(42);
    dos.writeDouble(3.14);
}

Practical case
You can use binary I/O streams to read and write images File:

// 读取图像文件
BufferedImage image = ImageIO.read(new File("image.jpg"));

// 将图像写入二进制文件
try (FileOutputStream fos = new FileOutputStream("image.bin")) {
    ImageIO.write(image, "jpg", fos);
}

Conclusion
Java I/O streams via FileInputStream, FileOutputStream, DataInputStream and DataOutputStream provides a powerful mechanism for processing binary data. These streams allow you to read and write primitive data types, strings, and complex objects, which are useful in a variety of applications.

The above is the detailed content of How do Java I/O streams handle 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