1 / 12

Java Serialization

Java Serialization. B.Ramamurthy. Introduction. The capability to store and retrieve Java objects is essential to building persistence and streaming into application. In this lecture we will discuss details about Serialization and saving and restoring objects in files. Topics for Discussion.

lea
Download Presentation

Java Serialization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Serialization B.Ramamurthy B.Ramamurthy

  2. Introduction • The capability to store and retrieve Java objects is essential to building persistence and streaming into application. • In this lecture we will discuss details about Serialization and saving and restoring objects in files. B.Ramamurthy

  3. Topics for Discussion • Object Serialization • Writing to an Object Stream • Reading from an Object Stream • Selective Serialization • Exception Handling • Example B.Ramamurthy

  4. Persistence • The values of the data structures in a program are lost when the program terminates. • If you want the data to exist after program termination you need to store it in a file or in a database. • We will look at file input/output in this discussion. B.Ramamurthy

  5. Object Serialization • Key to storing and retrieving objects is representing the “state” of the objects in a serialized form sufficient to reconstruct the objects. Object <===>byte stream, other data to help in reconstruction • Converting an object into an organized byte form for storage, streaming etc. B.Ramamurthy

  6. How to use Serialization? 1. import java.io.*; 2. 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() thorws ClassNotFoundException, IOException; B.Ramamurthy

  7. Writing to an Object Stream // serilaize various objects into a file FileOutputStream f = new FileOutputStream(“tmp”); ObjectOutputStream s = new ObjectOutputStream(f); s.writeObject(“Today”); s.writeObject(new Date()); Theater t = new Theater(4,6,10); //4 shows, 6 rows, 10 cols s.writeObject(t); B.Ramamurthy

  8. Reading from an Object Stream // Deserialize a objects from a file FileInputStream inf = new FileInputStream(“tmp”); ObjectInputStream s1 = new ObjectInputStream(inf); //read the Object and cast to retrieve the // actual object String = (String)s1.readObject(); Data date = (Date)s1.readObject(); B.Ramamurthy

  9. Serialization URL http://java.sun.com/products//jdk/1.1/docs/guide/serialization/index.html B.Ramamurthy

  10. Exception Handling • When ever you deal with IO you need make sure the file creation was successful accessing the files. • So enclose creation of file object and stream object within try and catch. B.Ramamurthy

  11. Example try {… } catch(Exception ex) { ex.printStackTrace(); // print the exception stack details } B.Ramamurthy

  12. Exception URL http://java.sun.com/docs/books/tutorial/essential/exceptions/QandE/answers.html B.Ramamurthy

More Related