Forums Register Login

Serialization

+Pie Number of slices to send: Send
When we want to store something in a file...we basically use Serialization and the steps we write for that are :

1. FileOutputStream fs = new FileOutputStream("abc.txt"); //connect to the file.

2. ObjectOutputStream os = new ObjectOutputStream(fs);

3. os.writeObject(object1)

Can anyone explain the 2nd point...i mean what does it actually do....

+Pie Number of slices to send: Send
The second line uses a pattern called Decorator pattern which adds some extra functionallity to the FileOutputStream at the runtime but still acting on the same object.

Generally FileOutputStream is used to write some data to the file but in your case you want to write the object into a file...FileOutputStream itself can not do it,so you decorating the FileOutputStream using ObjectOutputStream and telling that whatever the ObjectOutputStream is sending send it to the file.

You can read about the Decorator pattern here
+Pie Number of slices to send: Send
So basically when i write the code which i had written above ...what it'll do is:

It'll write the data into a buffer and then it'll send the data to the FileOutputStream to connect to the file.

I've also attached an image file which i've created after reading the Decorator pattern 4 times...correct me if i'm wrong

And thanks a lot for the info....i didn't knw about this topic before....
Untitled.jpg
Decorator
[Thumbnail for Untitled.jpg]
+Pie Number of slices to send: Send
yes.

you are right.
I'm doing laundry! Look how clean this tiny ad is:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1048 times.
Similar Threads
Serialization problem
Serialization is not for staitics
Serialization in java
serialization example of kathy siera book(page no-471)
Serialization code Issue
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:52:12.