Java FileInputStream 类

java.io 包中的 FileInputStream 类可用于从文件中读取数据(以字节为单位)。

它扩展了 InputStream 抽象类。

Java FileInputStream is a subclass of InputStream class.

在我们学习 FileInputStream 之前,请确保您了解 Java 文件


创建 FileInputStream

为了创建文件输入流,我们必须首先导入 java.io.FileInputStream 包。导入包后,以下是在 Java 中创建文件输入流的方法。

1. 使用文件路径

FileInputStream input = new FileInputStream(stringPath);

在这里,我们创建了一个输入流,它将链接到由 path 指定的文件。

2. 使用文件对象

FileInputStream input = new FileInputStream(File fileObject);

在这里,我们创建了一个输入流,它将链接到由 fileObject 指定的文件。


FileInputStream 方法

FileInputStream 类提供了 InputStream 类中各种方法的实现。

read() 方法

  • read() - 从文件中读取单个字节
  • read(byte[] array) - 从文件中读取字节并将其存储在指定的 数组
  • read(byte[] array, int start, int length) - 从文件中读取等于 length 的字节数,并将其存储在指定数组的 start 位置开始的位置

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

This is a line of text inside the file.

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

import java.io.FileInputStream;

public class Main {

  public static void main(String args[]) {

     try {
        FileInputStream input = new FileInputStream("input.txt");

        System.out.println("Data in the file: ");

        // Reads the first byte
        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();
     }
  }
}

输出

Data in the file:
This is a line of text inside the file.

在上面的示例中,我们创建了一个名为 input 的文件输入流。输入流链接到 input.txt 文件。

FileInputStream input = new FileInputStream("input.txt");

为了从文件中读取数据,我们在 while 循环中使用了 read() 方法。


available() 方法

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

import java.io.FileInputStream;

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 input = new FileInputStream("input.txt");

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

         // Reads 3 bytes from the file
         input.read();
         input.read();
         input.read();

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

         input.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;

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 input = new FileInputStream("input.txt");

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

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

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

         // close() method
         input.close();
      }
      catch (Exception e) {
         e.getStackTrace();
      }
   }
}

输出

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

在上面的示例中,我们使用 skip() 方法跳过了文件输入流中的 5 个字节数据。因此,代表文本 "This " 的字节未从输入流中读取。


close() 方法

要关闭文件输入流,我们可以使用 close() 方法。调用 close() 方法后,我们无法再使用输入流读取数据。

在以上所有示例中,我们都使用了 close() 方法来关闭文件输入流。


FileInputStream 的其他方法

方法 描述
finalize() 确保调用 close() 方法
getChannel() 返回与输入流关联的 FileChannel 对象
getFD() 返回与输入流关联的文件描述符
mark() 标记输入流中已读取数据的字节位置
reset() 将控制返回到输入流中设置标记的位置

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

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

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

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

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