Java tutorial
import java.io.Externalizable; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; class PersonExt implements Externalizable { private String name = "Unknown"; private String gender = "Unknown"; private double height = Double.NaN; public PersonExt() { } public PersonExt(String name, String gender, double height) { this.name = name; this.gender = gender; this.height = height; } public String toString() { return "Name: " + this.name + ", Gender: " + this.gender + ", Height: " + this.height; } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { this.name = in.readUTF(); this.gender = in.readUTF(); } public void writeExternal(ObjectOutput out) throws IOException { out.writeUTF(this.name); out.writeUTF(this.gender); } } public class Main { public static void main(String[] args) { PersonExt p1 = new PersonExt("John", "Male", 6.7); PersonExt p2 = new PersonExt("Wally", "Male", 5.7); PersonExt p3 = new PersonExt("Katrina", "Female", 5.4); File fileObject = new File("personext.ser"); try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileObject))) { oos.writeObject(p1); oos.writeObject(p2); oos.writeObject(p3); System.out.println(p1); System.out.println(p2); System.out.println(p3); } catch (IOException e1) { e1.printStackTrace(); } fileObject = new File("personext.ser"); try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileObject))) { p1 = (PersonExt) ois.readObject(); p2 = (PersonExt) ois.readObject(); p3 = (PersonExt) ois.readObject(); // Let's display the objects that are read System.out.println(p1); System.out.println(p2); System.out.println(p3); // Print the input path System.out.println("Objects were read from " + fileObject.getAbsolutePath()); } catch (Exception e) { e.printStackTrace(); } } }