Serialization

Serialization - Object Serialization In Java Serialization...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Object Serialization In Java
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Serialization is a function unique to Java that allows us to "save" an object on the disk. And of course we can "de-serialize" it later to restore it from disk to memory. (Kind of like doing an "old" instead of a "new" to create an object!) Since it is "Java Beans" objects that hold data, serialization is usually associated with beans. (We typically don't try to save and restore an application program.) Let's look at an example to see how this works. Here are a couple of simple (and identical except for the class name!) Java beans: Student public class Student implements java.io.Serializable { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } Instructor import java.io.Serializable; public class Instructor implements Serializable { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } Using Serialization for Data Persistence
Background image of page 2
Now, if we wanted to save these objects on disk (rather than create a "flat file" to save the bean data) we would use an I/O class called ObjectOutputStream . (Appropriate class name, huh!) And, to write the object on the disk, we would provide as the low-level, device-specific I/O class: FileOutputStream (another appropriate class name!). We will provide the address of the FileOutputStream to the constructor of the ObjectOutputStream. This tells the ObjectOutputStream: "When you have finished serializing an object in memory, give it to the FileOutputStream, who will write it on the disk". ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream("objects.ser")); The writeObject() method of the ObjectOutputStream will do the actual serialization and writing of the object for us. (An appropriate method name, eh!?) So if we had a Java bean called (at address) student , we would simply say oos.writeObject(student); to write the student object on the disk! Now the writeObject() method is very polite, and before it "externalizes" an object by writing it on the disk or sending it over the network, it asks the object if it's OK (!). The writeObject() method does this by using the instanceof operator to test the object for type Serializable . e.g. the writeObject() method has code that says if (student instanceof Serializable) go-ahead . .. else throw new SerializationException("student not serializable"); So a Java bean must "give permission" to be serialized by taking upon itself the type Serializable in addition to whatever other types it is. The FBI object, which contains the names of all the secret agents, may not want to be externalized, thank you! Nor the Visa object, which contains the passwords for all the accounts! The field data in the .ser file is not encrypted - you can see it with the Notepad editor! So an object does not "default" to being Serializable. But to be a "Java bean", the object MUST be serializable. Java provides a compromize with the keyword
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 9

Serialization - Object Serialization In Java Serialization...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online