Java FileInputStream和ObjectInputStream之间的区别

Java FileInputStream和ObjectInputStream之间的区别

在Java中处理文件时,有不同的类可用于从文件中读取数据。两个常用的类是FileInputStream和ObjectInputStream。虽然这两个类都用于从文件中读取数据,但它们在方法和功能上有所不同。在本文章中,我们将探讨FileInputStream和ObjectInputStream之间的区别,以及何时使用它们。

语法

在深入了解区别之前,让我们先了解FileInputStream和ObjectInputStream的语法:

FileInputStream语法

FileInputStream fis = new FileInputStream("file.txt");

ObjectInputStream 语法

FileInputStream fis = new FileInputStream("file.txt");
ObjectInputStream ois = new ObjectInputStream(fis);

语法解释

FileInputStream类能够从记录中读取原始字节。它以记录标题或记录描述符作为参数,并打开一个读取流。另一方面,ObjectInputStream可以作为更高级的课程来扩展FileInputStream。它提供额外的功能来从记录中读取序列化对象。为了使用ObjectInputStream,我们需要先创建一个FileInputStream的实例,然后将其作为参数传递给ObjectInputStream的构造函数。

方法1:FileInputStream

FileInputStream类在字节级别工作,主要用于从记录中读取原始数据。它提供了一个可以连续读取的字节流。让我们详细了解这种方法-

步骤

  • 通过提供文件名或文件描述符创建一个FileInputStream的实例。

  • 创建一个用于存储从文件中读取的数据的缓冲区或字节数组。

  • 使用FileInputStream的read()方法从文件中将数据读取到缓冲区中。

  • 根据需要处理缓冲区中的数据。

示例

import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamExample {
   public static void main(String[] args) {
      try {
         FileInputStream fis = new FileInputStream("file.txt");
         byte[] buffer = new byte[1024];
         int bytesRead = fis.read(buffer);
         while (bytesRead != -1) {
            // Process the data from the buffer
            System.out.println(new String(buffer, 0, bytesRead));
            bytesRead = fis.read(buffer);
         }
         fis.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

输出

This is an example text file.
It contains some sample data
to demonstrate the usage of FileInputStream.

在这个代码示例中,我们利用文件名“file.txt”创建了一个FileInputStream的实例。我们还创建了一个名为“buffer”的字节数组来存储从文件读取的数据。在while循环内部,我们利用FileInputStream的read()方法从文件中读取数据到缓冲区。然后我们处理缓冲区中的数据,这里我们只是简单地将它打印到控制台上。循环会一直进行,直到read()方法返回-1,表示文件的结尾。最后,我们关闭FileInputStream以释放系统资源。

方法2:ObjectInputStream

ObjectInputStream类用于从文件中读取序列化对象。它提供了更高级的功能来处理对象的序列化和反序列化。让我们来探索这种方法:

步骤

  • 通过提供文件名或文件描述符来创建FileInputStream的实例。

  • 通过将FileInputStream实例作为参数传递给ObjectInputStream来创建ObjectInputStream的实例。

  • 使用ObjectInputStream的readObject()方法从文件中读取序列化对象。

  • 根据需要处理对象。

示例

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class ObjectInputStreamExample {
   public static void main(String[] args) {
      try {
         FileInputStream fis = new FileInputStream("file.ser");
         ObjectInputStream ois = new ObjectInputStream(fis);
         Object obj = ois.readObject();
         // Process the object as required
         System.out.println(obj.toString());
         ois.close();
      } catch (IOException | ClassNotFoundException e) {
         e.printStackTrace();
      }
   }
}

输出

This is an example text file.
It contains some sample data
to demonstrate the use of FileInputStream.

在这个代码示例中,我们使用文件名“file.txt”创建了一个FileInputStream实例。然后,我们通过将FileInputStream实例作为参数来创建了一个ObjectInputStream实例。利用ObjectInputStream的readObject()方法,我们从文件中读取了序列化对象。然后,我们可以根据需要处理该对象。在这种情况下,我们只是使用toString()方法将对象打印到控制台。最后,我们关闭ObjectInputStream以释放系统资源。

Java中FileInputStream和ObjectInputStream的区别

差异点 FileInputStream ObjectInputStream
目的 用于从文件中读取原始字节。 用于从文件中读取序列化对象。
流类型 在字节级别上操作,提供字节流。 扩展了FileInputStream的高级类。
数据处理 需要额外的处理以将字节转换为所需的数据类型。 自动处理对象的序列化和反序列化。
与非序列化数据的使用 适用于从文件中读取任何类型的数据。 不适用于读取非序列化数据。
依赖关系 直接依赖于FileInputStream类。 扩展了FileInputStream类以提供额外功能。

结论

总体而言,Java中的FileInputStream和ObjectInputStream类提供了不同的方法来从文件中读取信息。FileInputStream用于读取原始字节,而ObjectInputStream用于读取序列化对象。了解这些类之间的区别和它们各自的使用情况对于在Java中有效地处理文件非常重要。通过根据您的需求使用适当的类,您可以在Java应用程序中成功地读取和处理文件中的数据。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程