A Serialization Example in Java

By: Fazal Viewed: 158837 times  Printer Friendly Format    


The following program illustrates how to use object serialization and deserialization. It begins by instantiating an object of class MyClass. This object has three instance variables that are of types String, int, and double. This is the information we want to save and restore.

A FileOutputStream is created that refers to a file named "serial," and an ObjectOutputStream is created for that file stream. The writeObject() method of ObjectOutputStream is then used to serialize our object. The object output stream is flushed and closed.

A FileInputStream is then created that refers to the file named "serial," and an ObjectInputStream is created for that file stream. The readObject() method of ObjectInputStream is then used to deserialize our object. The object input stream is then closed.

Note that MyClass is defined to implement the Serializable interface. If this is not done, a NotSerializableException is thrown. Try experimenting with this program by declaring some of the MyClass instance variables to be transient. That data is then not saved during serialization.

import java.io.*;
public class SerializationDemo {
public static void main(String args[]) {
// Object serialization
try {
MyClass object1 = new MyClass("Hello", -7, 2.7e10);
System.out.println("object1: " + object1);
FileOutputStream fos = new FileOutputStream("serial");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object1);
oos.flush();
oos.close();
}
catch(Exception e) {
System.out.println("Exception during serialization: " + e);
System.exit(0);
}
// Object deserialization
try {
MyClass object2;
FileInputStream fis = new FileInputStream("serial");
ObjectInputStream ois = new ObjectInputStream(fis);
object2 = (MyClass)ois.readObject();
ois.close();
System.out.println("object2: " + object2);
}
catch(Exception e) {
System.out.println("Exception during deserialization: " +
e);
System.exit(0);
}
}
}
class MyClass implements Serializable {
String s;
int i;
double d;
public MyClass(String s, int i, double d) {
this.s = s;
this.i = i;
this.d = d;
}
public String toString() {
return "s=" + s + "; i=" + i + "; d=" + d;
}
}

This program demonstrates that the instance variables of object1 and object2 are
identical. The output is shown here:

object1: s=Hello; i=-7; d=2.7E10
object2: s=Hello; i=-7; d=2.7E10

This tutorial is an extract from the "The Complete Reference Part 2 by Herbert Schildt".



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. short and precise! very nice
View Tutorial          By: Al at 2011-08-10 15:55:34

2. very nice tutorial it cleared my all doubts about
View Tutorial          By: Madhumati at 2011-09-14 06:08:43

3. this is simple and informative.thanks
View Tutorial          By: amrita at 2011-11-12 14:32:58

4. why we user serialization
View Tutorial          By: sonia at 2011-12-08 08:25:25

5. basic example on serialization... its good and cle
View Tutorial          By: Rag at 2012-01-08 02:53:41

6. In which stage the serialization is achieved . And
View Tutorial          By: siva at 2012-03-16 04:48:30

7. Thanks this tutorial helped me :)
View Tutorial          By: Anuja at 2012-07-17 06:59:39

8. Thanks, Good Example.
View Tutorial          By: Nobel at 2012-08-13 06:57:47

9. Very good article thanks for sharing.
View Tutorial          By: priti at 2012-08-29 11:32:07

10. Thnks, such a basic example to understand. Keep it
View Tutorial          By: Pradeep Singh at 2012-10-11 04:24:54

11. short and simple program
hel

View Tutorial          By: sourabh agrawal at 2012-10-16 04:55:27

12. Do the serialized output is displayed in the file
View Tutorial          By: Atihs at 2012-11-17 02:38:52

13. LIFE SAVER, thank you SO MUCH! :D
View Tutorial          By: Yey!! at 2013-01-15 00:49:31

14. thanks for sharing.It's Easy to Understand
View Tutorial          By: Ganesh Abothula at 2013-01-23 10:43:55

15. i want to more explanation about serlization
View Tutorial          By: sumathi at 2013-12-22 15:00:20

16.
View Tutorial          By: at 2017-06-10 02:27:07

17. Derekfab
View Tutorial          By: Derekfab at 2017-06-12 03:41:04

18. Jasonmef
View Tutorial          By: Jasonmef at 2017-06-25 08:22:24