亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> java

Java BufferedInputStream 类

Java BufferedInputStream 类

在本教程中,我们将通过示例了解 Java BufferedInputStream 及其方法。

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

它扩展了 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);

在上面的例子中,我们创建了一个 BufferdInputStream 命名为 缓冲区FileInputStream 命名为 文件 .

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

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

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


BufferedInputStream的方法

BufferedInputStream 类为 InputStream 中存在的不同方法提供实现 类。

read() 方法

假设我们有一个名为 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 官方文档)。


java

  1. Java final 关键字
  2. Java instanceof 运算符
  3. Java 继承
  4. Java 嵌套静态类
  5. Java 匿名类
  6. Java 单例类
  7. Java 反射
  8. Java ArrayList 类
  9. Java ObjectOutputStream 类
  10. Java BufferedReader 类
  11. Java 泛型
  12. Java 文件类