top of page
back.jpg
crystgandhi

Serialization vs Deserialization




Serialization

Deserialization

  ObjectOutputStream class is used to serialize an object to a byte stream.

ObjectInputStream class is used to deserialize a byte stream into an object

Commonly used for saving object states persistently, transmitting objects over a network, or sharing data between different applications.

Crucial for scenarios where data, previously serialized, needs to be retrieved and used in its original object form. Common use cases include reading objects from files, receiving serialized data over a network, or reconstructing objects stored in databases.

Object to Byte Stream Example

// Serialize an object to a byte stream
ByteArrayOutputStream bytArrOPStream = new ByteArrayOutputStream();
ObjectOutputStream objOPStream = new bjectOutputStream(bytArrOPStream);

// Example object to be serialized
MyObject myObject = new MyObject();

// Serialize the object
objOPStream.writeObject(myObject);

// Get the byte stream
byte[] byteStream = bytArrOPStream.toByteArray();

// Close streams
objOPStream.close();
bytArrOPStream.close();

Byte Stream to Object Example

// Deserialize an object from a byte stream
ByteArrayInputStream bytArrIPStream = new ByteArrayInputStream(byteStream);
ObjectInputStream objIPStream = new ObjectInputStream(bytArrIPStream);

// Deserialize the object
MyObject deserializedObject = (MyObject) objIPStream.readObject();

// Close streams
objectInputStream.close();
bytArrIPStream.close();

5 views0 comments

Recent Posts

See All

Test case for Valid Coupon Code

This test case checks whether a valid coupon code is successfully applied to a booking cart, the total is updated accordingly, and a...

Keyboard shortcut keys for various operations

General Shortcuts: Ctrl + C: Copy Ctrl + X: Cut Ctrl + V: Paste Ctrl + Z: Undo Ctrl + Y: Redo Ctrl + A: Select All Ctrl + S: Save Ctrl +...

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page