Serialization
Def : Serialization in java is a mechanism of writing the state of an object into a byte stream.
Used In : It is mainly used in Hibernate, RMI, JPA, EJB, JMS technologies.
DeSerialization
Def : The reverse operation of serialization is called deserialization.
The String class and all the wrapper classes implements java.io.Serializable interface by default.
Serializable is a marker interface (has no body). It is just used to "mark" java classes which support a certain capability.
Example of Java Serialization
import java.io.Serializable;
public class Employee implements Serializable {
public String name;
public void mailCheck() {
System.out.println("Mailing a check to " + name + " " + name);
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class SerializeDemo {
public static void main(String[] args) {
Employee e = new Employee();
e.name = "Reyan Ali";
try {
FileOutputStream fileOut = new FileOutputStream("employee.txt");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in employee.txt");
}
catch (IOException i) {
i.printStackTrace();
}
}
}
O/P : Serialized data is saved in employee.txt
import java.io.FileInputStream;
O/P : Name: Reyan Ali is printed in console
Def : Serialization in java is a mechanism of writing the state of an object into a byte stream.
Used In : It is mainly used in Hibernate, RMI, JPA, EJB, JMS technologies.
DeSerialization
Def : The reverse operation of serialization is called deserialization.
The String class and all the wrapper classes implements java.io.Serializable interface by default.
Advantage of Java Serialization
It is mainly used to travel object's state on the network (known as marshaling).Serializable is a marker interface (has no body). It is just used to "mark" java classes which support a certain capability.
ObjectOutputStream class
An ObjectOutputStream is used to write primitive data types and Java objects to an OutputStream.Only objects that support the java.io.Serializable interface can be written to streams.
Constructor
1) public ObjectOutputStream(OutputStream out) throws IOException {}:-creates an ObjectOutputStream that writes to the specified OutputStream.Important Methods
1) public final void writeObject(Object obj) throws IOException {} :-writes the specified object to the ObjectOutputStream. |
2) public void flush() throws IOException {}:-flushes the current output stream. |
3) public void close() throws IOException {} :-closes the current output stream. |
ObjectInputStream class
An ObjectInputStream deserializes objects and primitive data written using an ObjectOutputStream.Constructor
1) public ObjectInputStream(InputStream in) throws IOException {} :-creates an ObjectInputStream that reads from the specified InputStream. |
Important Methods
1) public final Object readObject() throws IOException, ClassNotFoundException{} :-reads an object from the input stream. |
2) public void close() throws IOException {}:-closes ObjectInputStream. |
Example of Java Serialization
import java.io.Serializable;
public class Employee implements Serializable {
public String name;
public void mailCheck() {
System.out.println("Mailing a check to " + name + " " + name);
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class SerializeDemo {
public static void main(String[] args) {
Employee e = new Employee();
e.name = "Reyan Ali";
try {
FileOutputStream fileOut = new FileOutputStream("employee.txt");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in employee.txt");
}
catch (IOException i) {
i.printStackTrace();
}
}
}
O/P : Serialized data is saved in employee.txt
No comments:
Post a Comment