CSE116 A,B Introduction to Computer Science II Fall 2000 Lab5 Objective: To learn about Serialization that provides object read and write capability to applications. Introduction: Capability to store and retrieve objects is essential to adding persistence and streaming into application. Key to storing and retrieving objects is representing the "state" of the objects in a serialized form sufficient to reconstruct the objects. Objects < == > byte stream, other data to help in reconstruction. Using Serialization: 1. import java.io.*; 2. Objects to be written/read will have to implement java.io.Serialization interface. 3. Use writeObject and readObject methods whose header are as given below: void writeObject(Object obj) throws IOException Object readObject() throws ClassNotFoundException, IOException 4. Serialize various objects into a file Example: FileOutputStream f = new FileOutputStream("tmp"); ObjectOutputStream s = new ObjectOutputStream(f); s.writeObject("Today"); s.writeObject(new Date()); Location loc = new Location(3.4,5.6); // Location should implement Serializable interface s.writeObject(); 5. Reading from an Object stream. Example: FileInputStream inf = new FileInputStream("tmp"); ObjectInputStream s1 = new ObjectInputStream(inf); String s = (String) s1.readObject(); Date date = (Date)s1.readObject(); 6. See Lab4 and observe how Record object is input and output into a file. What to do for lab5? Rewrite Location class to implement java.io.Serializable. Location class is available at /projects/bina/cse116/edu/colorado/geometry directory. Create a LinkedList of Location objects. Let the Linked List have at least 4 objects. Write the linked list into a file. Read from the file and print it out to standard output.