File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Serialization" Watch "Serialization" New topic
Author

Serialization

Vasiq Molvizadah
Ranch Hand

Joined: Dec 24, 2009
Posts: 66
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....


Human Knowledge Belongs to the World.
- Vasiq Mz
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
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

SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Vasiq Molvizadah
Ranch Hand

Joined: Dec 24, 2009
Posts: 66
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....



[Thumbnail for Untitled.jpg]

Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
yes.

you are right.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serialization
 
Similar Threads
Serialization problem
serialization example of kathy siera book(page no-471)
Serialization is not for staitics
Serialization code Issue
Serialization in java