Java BufferedInputStream 类

java.io包中的BufferedInputStream类用于与其他输入流结合使用,以更有效地读取(以字节为单位)数据。

它扩展了 InputStream 抽象类。

The BufferedInputStream class is a subclass of the Java InputStream.

BufferedInputStream 的工作原理

BufferedInputStream 维护一个内部的8192 字节缓冲区

BufferedInputStream的读取操作期间,一部分字节从磁盘读取并存储在内部缓冲区中。然后从内部缓冲区逐个读取字节。

因此,与磁盘的通信次数减少了。这就是为什么使用BufferedInputStream读取字节更快的原因。


创建 BufferedInputStream

要创建BufferedInputStream,我们必须首先导入java.io.BufferedInputStream包。导入包后,可以按如下方式创建输入流。

// Creates a FileInputStream
FileInputStream file = new FileInputStream(String path);

// Creates a BufferedInputStream
BufferedInputStream buffer = new BufferInputStream(file);

在上面的示例中,我们使用名为 fileFileInputStream创建了一个名为 bufferBufferdInputStream

这里,内部缓冲区的默认大小为 8192 字节。但是,我们也可以指定内部缓冲区的大小。

// Creates a BufferedInputStream with specified size internal buffer
BufferedInputStream buffer = new BufferInputStream(file, int size);

buffer 将有助于更快地从文件中读取字节。


BufferedInputStream 的方法

BufferedInputStream 类实现了InputStream类中的各种方法。

read() 方法

  • read() - 从输入流中读取单个字节
  • read(byte[] arr) - 从流中读取字节并存储在指定的 数组
  • read(byte[] arr, int start, int length) - 从流中读取等于 length 的字节数,并从 start 位置开始存储在指定的数组中

假设我们有一个名为 input.txt 的文件,其内容如下。

This is a line of text inside the file.

让我们尝试使用BufferedInputStream读取文件。

import java.io.BufferedInputStream;
import java.io.FileInputStream;

class Main {
    public static void main(String[] args) {
        try {

            // Creates a FileInputStream
            FileInputStream file = new FileInputStream("input.txt");

            // Creates a BufferedInputStream
            BufferedInputStream input = new BufferedInputStream(file);

            // Reads first byte from file
            int i = input .read();

            while (i != -1) {
                System.out.print((char) i);

                // Reads next byte from the file
                i = input.read();
            }
            input.close();
        }

        catch (Exception e) {
            e.getStackTrace();
        }
    }
}

输出

This is a line of text inside the file.

在上面的示例中,我们创建了一个名为 buffer 的缓冲输入流,以及一个FileInputStream。输入流与文件input.txt关联。

FileInputStream file = new FileInputStream("input.txt");
BufferedInputStream buffer = new BufferedInputStream(file);

在这里,我们使用了read()方法从缓冲读取器的内部缓冲区读取字节数组。


available() 方法

要获取输入流中可用的字节数,我们可以使用available()方法。例如,

import java.io.FileInputStream;
import java.io.BufferedInputStream;

public class Main {

   public static void main(String args[]) {

      try {

         // Suppose, the input.txt file contains the following text
         // This is a line of text inside the file.
         FileInputStream file = new FileInputStream("input.txt");
         
         // Creates a BufferedInputStream
         BufferedInputStream buffer = new BufferedInputStream(file);

         // Returns the available number of bytes
         System.out.println("Available bytes at the beginning: " + buffer.available());

         // Reads bytes from the file
         buffer.read();
         buffer.read();
         buffer.read();

         // Returns the available number of bytes
         System.out.println("Available bytes at the end: " + buffer.available());

         buffer.close();
      }

      catch (Exception e) {
         e.getStackTrace();
      }
   }
}

输出

Available bytes at the beginning: 39
Available bytes at the end: 36

在上面的例子中:

  1. 我们首先使用available()方法检查输入流中可用的字节数。
  2. 然后,我们使用read()方法读取了 3 次,从输入流中读取了 3 个字节。
  3. 现在,读取字节后,我们再次检查了可用字节数。这次可用字节数减少了 3。

skip() 方法

要丢弃并跳过指定的字节数,我们可以使用 skip() 方法。例如,

import java.io.FileInputStream;
import java.io.BufferedInputStream;

public class Main {

   public static void main(String args[]) {

      try {
         // Suppose, the input.txt file contains the following text
         // This is a line of text inside the file.
         FileInputStream file = new FileInputStream("input.txt");

         // Creates a BufferedInputStream
         BufferedInputStream buffer = new BufferedInputStream(file);

         // Skips the 5 bytes
         buffer.skip(5);
         System.out.println("Input stream after skipping 5 bytes:");

         // Reads the first byte from input stream
         int i = buffer.read();
         while (i != -1) {
            System.out.print((char) i);

            // Reads next byte from the input stream
            i = buffer.read();
         }

         // Closes the input stream
         buffer.close();
      }

      catch (Exception e) {
         e.getStackTrace();
      }
   }
}

输出

Input stream after skipping 5 bytes: is a line of text inside the file.

在上面的示例中,我们使用skip()方法跳过了文件输入流中的 5 个字节。因此,字节 'T''h''i''s'' ' 被从输入流中跳过了。


close() 方法

要关闭缓冲输入流,我们可以使用close()方法。调用close()方法后,我们就不能再使用该输入流来读取数据了。


BufferedInputStream 的其他方法

方法 描述
mark() 标记输入流中已读取数据的游标位置
reset() 将控制返回到输入流中设置标记的位置

要了解更多信息,请访问 Java BufferdInputStream(官方 Java 文档)


另请阅读

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战