import java.io.Serializable; public class Person implements Serializable { private String name; private int age; } import java.io.*; public class SerializationExample { public static void main(String[] args) { try { FileOutputStream fileOut = new FileOutputStream("person.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(person); out.close(); fileOut.close(); } catch (IOException e) { e.printStackTrace(); } Person deserializedPerson = null; try { FileInputStream fileIn = new FileInputStream("person.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); deserializedPerson = (Person) in.readObject(); in.close(); fileIn.close(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } if (deserializedPerson != null) { } } }


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