public String toString(){ return name + "," + job; } }
publicclassSerializationTest{ publicstaticvoidmain(String[] args){ Person personLee = new Person("이순신", "대표"); Person personKim = new Person("김유신", "상무이사");
try (FileOutputStream fos = new FileOutputStream("serial.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos)) { // Serialization oos.writeObject(personLee); oos.writeObject(personKim);
} catch (IOException e) { e.printStackTrace(); }
try (FileInputStream fis = new FileInputStream("serial.txt"); ObjectInputStream ois = new ObjectInputStream(fis)) { Person pLee = (Person)ois.readObject(); Person pKim = (Person)ois.readObject(); System.out.println(pLee); System.out.println(pKim); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); }
publicclassExternalizableTest{ publicstaticvoidmain(String[] args){ People peopleLee = new People("이순신", "대표"); People peopleKim = new People("김유신", "상무이사");