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

Java FileInputStream 类

Java FileInputStream 类

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

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

它扩展了 InputStream 抽象类。

在我们了解 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() 方法

假设我们有一个名为 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");

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


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 官方文档)。


java

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