Home  >  Article  >  Java  >  What are the principles and key concepts of Java I/O streams?

What are the principles and key concepts of Java I/O streams?

WBOY
WBOYOriginal
2024-04-15 11:12:021125browse

Java I/O streams provide a mechanism for reading and writing data through a producer-consumer model, simplifying communication between data sources and destinations. Stream types are divided into byte streams and character streams. The input stream is used to read data, and the output stream is used to write data. Key concepts include buffering, marking, read/write methods, and file handles. Practical case: When reading text from a file and copying it to another file, FileInputStream is used as the input stream, FileOutputStream is used as the output stream, the while loop reads bytes and writes them, and the flush() method refreshes the buffer.

Java I/O流的原理和关键概念是什么?

Principles and key concepts of Java I/O streams

Introduction

The Java I/O stream mechanism provides an abstraction for reading and writing data, simplifying communication between different types of data sources and destinations. It is based on the producer-consumer model, where producers generate data and consumers receive and process it.

Types of streams

Byte stream: Read and write raw byte data, such as pictures or files.
Character stream: Read and write character data, represented by the Char type, such as text or code.

Input and output streams

Input stream: Used to read data from the data source.
Output stream: Used to write data to the destination.

Key Concepts

  • Buffering: To improve performance, streams use buffers to temporarily store data.
  • Markers: Markers allow the stream to remember its current position so that the data can be re-read later.
  • Read and write methods: Streams provide various methods to read and write data, such as read(), write() and flush().
  • File handle: For file I/O, a file handle is created when a file is opened, which is used to identify the file.

Practical example:

Consider the following Java code to read text from a file and copy it to another file:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyFile {

    public static void main(String[] args) {
        // 源文件
        File sourceFile = new File("source.txt");

        // 目标文件
        File targetFile = new File("target.txt");

        // 创建文件输入流
        try (FileInputStream fis = new FileInputStream(sourceFile);
             // 创建文件输出流
             FileOutputStream fos = new FileOutputStream(targetFile)) {

            // 循环读取源文件中的字节
            int b;
            while ((b = fis.read()) != -1) {

                // 将读取到的字节写入目标文件
                fos.write(b);
            }

            // 刷新目标文件缓冲区
            fos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In this example:

  • FileInputStream is used as the input stream for reading from source.txt Read bytes from the file.
  • FileOutputStream is used as an output stream for writing bytes into the target.txt file.
  • while Loops to read the bytes in source.txt and write them to target.txt. The
  • flush() method flushes the buffer of target.txt, ensuring that all bytes have been written to the file.

The above is the detailed content of What are the principles and key concepts of Java I/O streams?. 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