import java.io.*; import java.util.*; class Record implements Serializable { private int id; private String name; public Record(int id, String name) { setID(id); setName(name); } public void setID(int id) { this.id= id; } public void setName(String name) { if (name == null) this.name = ""; else this.name = name; } public int getID() { return id; } public String getName() { return name; } public String toString() { return String.format("%04d-> %s", id, name); } } public class TestSeq4 { public static void main(String[] args) { ObjectInputStream in = null; int readCount = 0; int writeCount = 0; System.out.printf("Records found in file:\n"); try { in = new ObjectInputStream(new FileInputStream("records3.txt")); while (true) { try { Record r = (Record) in.readObject(); readCount++; System.out.println(r); writeCount++; } catch (EOFException e2) { // this is expected and OK break; } } } catch (Exception e1) { System.err.println(e1.getMessage()); } if (in != null) { try { in.close(); } catch (IOException e) {} } System.out.println("\nRecords read: " + readCount); System.out.println("Records written: " + writeCount); } }