Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

We'll meet more different types of streams, readers, and writers in the tutorials on Inheritance, Networking, Multi-threading, and others coming up.

ObjectInputStream and ObjectOutputStream sugar

There are also helper methods for Object input/output classes as this example shows:

Code Block

@Immutable class Point implements Serializable { int x, y }

def file = new File('points.dat')
def square = [ new Point(10, 10),
               new Point(20, 10),
               new Point(20, 20),
               new Point(10, 20) ]
file.withObjectOutputStream { oos ->
    oos.writeObject(square)
}
file.withObjectInputStream(getClass().classLoader){ ois ->
    def saved = ois.readObject( )
    assert square == saved
}