(1)创建一个 `ObjectInputStream` 输入流 `ois`
(2)调用此输入流 `ois` 的 `readObject()` 方法读取对象
详细代码如下所示:
packagecom.xiaowang.java.serializable; importjava.io.FileInputStream; importjava.io.FileNotFoundException; importjava.io.IOException; importjava.io.ObjectInputStream; /*** 反序列化Student对象**/publicclassDeserializeTest { publicstaticvoidmain(String[] args) { Studentstu=null; try { FileInputStreamfis=newFileInputStream("/root/software/student.ser"); ObjectInputStreamois=newObjectInputStream(fis); // 从流中取出下一个对象,并将对象反序列化// 返回值类型为Object,因此需要将它转换成合适的数据类型,这里转换成Student类型stu= (Student) ois.readObject(); ois.close(); fis.close(); } catch (FileNotFoundExceptione) { e.printStackTrace(); } catch (ClassNotFoundExceptione) { System.out.println("Student class not found!!!"); e.printStackTrace(); } catch (IOExceptione) { e.printStackTrace(); } System.out.println("Deserialized Student..."); System.out.println(stu); } }