import java.io.*; public class SerializationDemo { public static void main(String[] args) { Student student = new Student("Alice", 20); try { FileOutputStream fileOut = new FileOutputStream("student.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(student); out.close(); fileOut.close(); } catch (IOException e) { e.printStackTrace(); } try { FileInputStream fileIn = new FileInputStream("student.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); Student deserializedStudent = (Student) in.readObject(); in.close(); fileIn.close(); e.printStackTrace(); } } } class Student implements Serializable { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } }


上一篇:
下一篇:
切换中文