import java.io.*; public class WriteReadList { Node listRoot = new Node("root"); public WriteReadList() { /* Create the list. */ Node next, last = listRoot; for (int i=0; i<10; i++) { next = new Node("node no " + i); last.setNext(next); last = next; } } public static void main(String[] args) { WriteReadList wrl = new WriteReadList(); System.out.println("before saving:"); wrl.printList(); /* Stream the list to file. */ try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("list.ser")); oos.writeObject(wrl.listRoot); } catch (Exception e) { e.printStackTrace(); } wrl.listRoot = null; System.out.println("after saving, before reading:"); wrl.printList(); try { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("list.ser")); wrl.listRoot = (Node)ois.readObject(); } catch (Exception e) { e.printStackTrace(); } System.out.println("after reading:"); wrl.printList(); } private void printList() { for (Node n=listRoot; n != null; n = n.getNext()) { System.out.println(n); } System.out.println(); } public static class Node implements Serializable { private String info; private Node next; public Node(String s) { info = s; } public Node getNext() { return next; } public void setNext(Node n) { next = n; } public String toString() { return info; } } //inner class Node } //class WriteReadList