A Serialization Example in Java
By: Fazal
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".
Archived Comments
1. Jasonmef
View Tutorial By: Jasonmef at 2017-06-25 08:22:24
2. Derekfab
View Tutorial By: Derekfab at 2017-06-12 03:41:04
3.
View Tutorial By: at 2017-06-10 02:27:07
4. i want to more explanation about serlization
View Tutorial By: sumathi at 2013-12-22 15:00:20
5. thanks for sharing.It's Easy to Understand
View Tutorial By: Ganesh Abothula at 2013-01-23 10:43:55
6. LIFE SAVER, thank you SO MUCH! :D
View Tutorial By: Yey!! at 2013-01-15 00:49:31
7. Do the serialized output is displayed in the file (in the form of bytes) in the file name mentioned?
View Tutorial By: Atihs at 2012-11-17 02:38:52
8. short and simple program
help bigenners
thanks!!!!
View Tutorial By: sourabh agrawal at 2012-10-16 04:55:27
9. Thnks, such a basic example to understand. Keep it up.
View Tutorial By: Pradeep Singh at 2012-10-11 04:24:54
10. Very good article thanks for sharing.
View Tutorial By: priti at 2012-08-29 11:32:07
11. Thanks, Good Example.
View Tutorial By: Nobel at 2012-08-13 06:57:47
12. Thanks this tutorial helped me :)
View Tutorial By: Anuja at 2012-07-17 06:59:39
13. In which stage the serialization is achieved . And what is persisting an object
View Tutorial By: siva at 2012-03-16 04:48:30
14. basic example on serialization... its good and clear
View Tutorial By: Rag at 2012-01-08 02:53:41
15. why we user serialization
View Tutorial By: sonia at 2011-12-08 08:25:25
16. this is simple and informative.thanks
View Tutorial By: amrita at 2011-11-12 14:32:58
17. very nice tutorial it cleared my all doubts about serialization
View Tutorial By: Madhumati at 2011-09-14 06:08:43
18. short and precise! very nice
View Tutorial By: Al at 2011-08-10 15:55:34
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Java program to get location meta data from an image
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program