原文: https://www.programiz.com/java-programming/objectinputstream
在本教程中,我们将借助示例学习 Java ObjectOutputStream及其方法。
java.io包的ObjectInputStream类可用于读取先前由ObjectOutputStream写入的对象。
它扩展了InputStream抽象类。

在学习ObjectInputStream类之前,请确保您了解ObjectOutputStream类。
ObjectInputStream的工作原理
ObjectInputStream主要用于读取ObjectOutputStream写入的数据。
基本上,ObjectOutputStream将 Java 对象转换为相应的流。 这称为序列化。 这些转换后的流可以存储在文件中,也可以通过网络传输。
现在,如果需要读取这些对象,将使用ObjectInputStream将流转换回相应的对象。 这称为反序列化。
创建一个ObjectInputStream
为了创建对象输入流,我们必须首先导入java.io.ObjectInputStream包。 导入包后,就可以创建输入流。
// Creates a file input stream linked with the specified fileFileInputStream fileStream = new FileInputStream(String file);// Creates an object input stream using the file input streamObjectInputStream objStream = new ObjectInputStream(fileStream);
在上面的示例中,我们创建了一个名为objStream的对象输入流,该对象输入流与名为fileStream的文件输入流链接在一起。
现在,objStream可用于从文件读取对象。
ObjectInputStream的方法
ObjectInputStream类提供InputStream类中存在的不同方法的实现。
read()方法
read()- 从输入流中读取一个字节的数据readBoolean()- 以布尔形式读取数据readChar()- 以字符形式读取数据readInt()- 读取整数形式的数据readObject()- 从输入流中读取对象
示例 1:Java ObjectInputStream
让我们看看如何使用ObjectInputStream类读取由ObjectOutputStream类编写的对象。
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;class Main {public static void main(String[] args) {int data1 = 5;String data2 = "This is programiz";try {FileOutputStream file = new FileOutputStream("file.txt");ObjectOutputStream output = new ObjectOutputStream(file);// Writing to the file using ObjectOutputStreamoutput.writeInt(data1);output.writeObject(data2);FileInputStream fileStream = new FileInputStream("file.txt");// Creating an object input streamObjectInputStream objStream = new ObjectInputStream(fileStream);//Using the readInt() methodSystem.out.println("Integer data :" + objStream.readInt());// Using the readObject() methodSystem.out.println("String data: " + objStream.readObject());output.close();objStream.close();}catch (Exception e) {e.getStackTrace();}}}
输出
Integer data: 5String data: This is programiz
在上面的示例中,我们使用readInt()和readObject()方法从文件中读取整数数据和对象数据。
在这里,我们使用ObjectOutputStream将数据写入文件。 然后,我们使用ObjectInputStream从文件中读取数据。
示例 2:Java ObjectInputStream
让我们看另一个实际的例子,
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;class Dog implements Serializable {String name;String breed;public Dog(String name, String breed) {this.name = name;this.breed = breed;}}class Main {public static void main(String[] args) {// Creates an object of Dog classDog dog = new Dog("Tyson", "Labrador");try {FileOutputStream file = new FileOutputStream("file.txt");// Creates an ObjectOutputStreamObjectOutputStream output = new ObjectOutputStream(file);// Writes objects to the output streamoutput.writeObject(dog);FileInputStream fileStream = new FileInputStream("file.txt");// Creates an ObjectInputStreamObjectInputStream input = new ObjectInputStream(fileStream);// Reads the objectsDog newDog = (Dog) input.readObject();System.out.println("Dog Name: " + newDog.name);System.out.println("Dog Breed: " + newDog.breed);output.close();input.close();}catch (Exception e) {e.getStackTrace();}}}
输出:
Dog Name: TysonDog Breed: Labrador
在上面的示例中,我们创建了
- 名为
output的ObjectOutputStream,使用名为file的文件 - 名为
input的ObjectInputStream,使用名为fileStream的FileInputStream Dog类的对象Dog
在这里,我们然后使用对象输出流将对象写入文件。 并且,对象输入流从文件中读取对象。
注意:Dog类实现Serializable接口。 这是因为ObjectOutputStream仅将可序列化的对象写入输出流。
ObjectInputStream的其他方法
| 方法 | 内容描述 |
|---|---|
available() |
返回输入流中的可用字节数 |
mark() |
标记输入流中已读取数据的位置 |
reset() |
将控件返回到输入流中设置了标记的点 |
skipBytes() |
从输入流中跳过并丢弃指定的字节 |
close() |
关闭对象输入流 |
要了解更多信息,请访问 Java ObjectInputStream(官方 Java 文档)。
