[Go to site: main page, start]

0% found this document useful (0 votes)
3 views8 pages

13 Java Serialization

Java Serialization is the process of converting an object into a byte array for storage or transmission, and deserialization is the reverse process. The Serializable interface is used to mark classes for serialization, while ObjectOutputStream and ObjectInputStream handle the writing and reading of objects to and from files. The document includes example code for writing and reading serialized objects, as well as notes on managing transient fields and sending objects over a network.

Uploaded by

smaseesshah786
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

13 Java Serialization

Java Serialization is the process of converting an object into a byte array for storage or transmission, and deserialization is the reverse process. The Serializable interface is used to mark classes for serialization, while ObjectOutputStream and ObjectInputStream handle the writing and reading of objects to and from files. The document includes example code for writing and reading serialized objects, as well as notes on managing transient fields and sending objects over a network.

Uploaded by

smaseesshah786
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Serialization

Introduction
• Serialization is simply turning an existing object into a byte array.
• This byte array represents
• the class of the object,
• the version of the object,
• and the internal state of the object.
• This byte array can be used to
• Store the state of the object for later retrieval (store in a file).
• Send object over a network to another JVM running the same code.
• Deserialization is converting a byte array back to an Object.
Serialization
• A marker interface called Serializable is provided.
• It is an empty interface, hence, classes implementing this interface are not
required to override any methods related to the interface.

• [Link](Object) traverses all the internal


references of the object recursively and writes all of them provided
classes associated with the objects have also implemented
Serializable.

• Any fields that the programmer does not want to serialize should be
marked as transient.
Object Storage in a File
• Classes ObjectInputStream and ObjectOutputStream are high-level
streams that contain the methods for serializing and deserializing an
object.
• Writing Objects to a File
1. FileOutputStream file = new FileOutputStream("[Link]");
2. The writeObject(Object obj) from the ObjectOutputStream class can be
used to write an object into a file
• Reading Objects from a File
Classes Implementing the Serializable Interface
public class Student implements Serializable { public class LibraryAccount implements Serializable {
private int Roll;
private int Id;
private String Name;
private LibraryAccount lib; private static int NumberAccounts=0;
private final static long serialVersionUID = 1000; public LibraryAccount(){
public Student(int Roll, String Name){ NumberAccounts++;
[Link]= Roll;
[Link]= NumberAccounts;
[Link] = Name;
} }
public int getRoll(){return [Link];} public int getId(){return [Link];}
public String getName(){return [Link];} public static int getNumberofAccounts() {
public LibraryAccount getLibAccount(){
return NumberAccounts;}
return [Link];}
public void createLibraryAccount(){ }
[Link] = new LibraryAccount();}
}
Writing Objects in a File
public class WriterClass {
public static void main(String[] args) {
Student std = new Student(1,"Muhammad Ali");
[Link]();
[Link]([Link]()+ " " + [Link]() + " " +
[Link]().getId());
try { /* A file is created/opened where we want to write an object. “ser” is normally a
convention that is followed for file names containing a serialized file.*/
FileOutputStream file = new FileOutputStream("[Link]");
/ *The writeObject(Object obj) from the ObjectOutputStream class can be used to
write an object into a file.*/
ObjectOutputStream out = new ObjectOutputStream(file);
[Link](std);
[Link]();
[Link]();}
catch (FileNotFoundException e) {[Link]();}
catch (IOException e) {[Link]();}
} }
Reading Object (Deserialization) from a File
public class ReaderClass {
public static void main(String[] args) {
try { /* The file is opened from where we want to read an object.*/
FileInputStream input = new FileInputStream(“[Link]”);
/ *The readObject() from the ObjectInputStream class can be used to
read an object from a file. The return object needs to be casted before use.*/
ObjectInputStream inStream = new ObjectInputStream(input);
Student obj = (Student) [Link]();
[Link]([Link]+ " " + [Link]);
[Link]();
[Link]();
}
catch (FileNotFoundException e) {[Link]();}
catch (IOException e) {[Link](); }
catch (ClassNotFoundException e) {[Link]();}
}
}
Sending Objects over a Network
• Example attached separately in the shared folder.

You might also like