Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialization

 
Vasiq Molvizadah
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....

 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vasiq Molvizadah
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
[Thumbnail for Untitled.jpg]
Decorator
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes.

you are right.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic