Java serialization example
The Seerializable interface does not have any method, so the implementation is still empty ., It is called the mark interface package serialization; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. objectInputStream; import java. io. objectOutputStream; import java. io. serializable; public class ObjectSerializationApp {public static void main (String [] args) {// TODO Auto-generated method stubObjectOutputStream objectWriter = null; ObjectInputStream objectReader = null; try {objectWriter = new ObjectOutputStream (new FileOutputStream ("student. dat "); objectWriter. writeObject (new Student (1, "John", "Mayor"); objectWriter. writeObject (new Student (2, "Sam", "Abel"); objectWriter. writeObject (new Student (3, "Anita", "Motwani"); System. out. println ("print the list that exists in the student database"); objectReader = new ObjectInputStream (new FileInputStream ("student. dat "); for (int I = 0; I <3; I ++) {// method readObject () returns a Student object, this Student object is also printed to the Console System by calling the implicit override toString () method. out. println (objectReader. readObject () ;}} catch (Exception e) {// TODO: handle finished tione. printStackTrace ();} finally {try {objectWriter. close (); objectReader. close ();} catch (Exception e2) {// TODO: handle implements tione2.printstacktrace () ;}}// Student class Student implements Serializable {private String firstName; private String lastName; private int id; public Student (int id, String firstName, String lastName) {// TODO Auto-generated constructor stubthis. id = id; this. firstName = firstName; this. lastName = lastName;} // rewrite the toString () method public String toString () {return ("ID:" + id + "" + firstName + "" + lastName );}}