Serialization in JAVA
Serialization in JAVA
Definition : java serialization is to write an object into a stream, so that it can be transported through a network and that object can be rebuilt again
Code :
package core;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class SerializationBox implements Serializable {
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class SerializationBox implements Serializable {
private int serializableProp = 100;
public int getSerializableProp() {
public int getSerializableProp() {
return serializableProp;
}
}
public class SerializationSample {
public static void main(String args[]) throws IOException, FileNotFoundException, ClassNotFoundException
{
SerializationBox serialB = new SerializationBox(10);
// serialization
serialize("C:\\Temp\\kumud.txt", serialB);
System.out.println("After Serialize");
{
SerializationBox serialB = new SerializationBox(10);
// serialization
serialize("C:\\Temp\\kumud.txt", serialB);
System.out.println("After Serialize");
//deserialization
SerializationBox sb = (SerializationBox) deSerialize("C:\\Temp\\kumud.txt");
System.out.println(sb.getSerializableProp());
System.out.println("Static Value"+sb.getStaticValue());
SerializationBox sb = (SerializationBox) deSerialize("C:\\Temp\\kumud.txt");
System.out.println(sb.getSerializableProp());
System.out.println("Static Value"+sb.getStaticValue());
}
public static void serialize(String outFile, Object serializableObject) throws IOException {
FileOutputStream fos = new FileOutputStream(outFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(serializableObject);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(serializableObject);
}
public static Object deSerialize(String serilizedObject) throws FileNotFoundException, IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream(serilizedObject);
ObjectInputStream ois = new ObjectInputStream(fis);
return ois.readObject();
FileInputStream fis = new FileInputStream(serilizedObject);
ObjectInputStream ois = new ObjectInputStream(fis);
return ois.readObject();
}
}
Output :
After Serialize
100
100
in above code when we serialize object and save into file C:\\Temp\\kumud.txt file and when desrialize agiain readObject convert into object and return function value 100
Concept : when you want to prevent to save the state during searialization , use Transient and static Key
package core;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class SerializationBox implements Serializable {
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class SerializationBox implements Serializable {
private int serializableProp = 100;
private transient int b=800;
public int getSerializableProp() {
private transient int b=800;
public int getSerializableProp() {
return serializableProp;
}
public int getNontransientValue() {
public int getNontransientValue() {
return b;
}
}
public class SerializationSample {
public static void main(String args[]) throws IOException, FileNotFoundException, ClassNotFoundException
{
SerializationBox serialB = new SerializationBox(10);
serialize("C:\\Temp\\kumud.txt", serialB);
System.out.println("After Serialize");
SerializationBox sb = (SerializationBox) deSerialize("C:\\Temp\\kumud.txt");
System.out.println(sb.getSerializableProp());
System.out.println("Transient Value"+sb. getNontransientValue);
{
SerializationBox serialB = new SerializationBox(10);
serialize("C:\\Temp\\kumud.txt", serialB);
System.out.println("After Serialize");
SerializationBox sb = (SerializationBox) deSerialize("C:\\Temp\\kumud.txt");
System.out.println(sb.getSerializableProp());
System.out.println("Transient Value"+sb. getNontransientValue);
}
public static void serialize(String outFile, Object serializableObject) throws IOException {
FileOutputStream fos = new FileOutputStream(outFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(serializableObject);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(serializableObject);
}
public static Object deSerialize(String serilizedObject) throws FileNotFoundException, IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream(serilizedObject);
ObjectInputStream ois = new ObjectInputStream(fis);
return ois.readObject();
FileInputStream fis = new FileInputStream(serilizedObject);
ObjectInputStream ois = new ObjectInputStream(fis);
return ois.readObject();
}
}
Output :
After Serialize
100
Transient Value 0
After Serialize
100
Transient Value 0
Concept :
In above transient value does not save , so when we deserialize the coming value is 0,
so there is question coming in mind that is constructor is run during deserializtion
so there is question coming in mind that is constructor is run during deserializtion
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.