Retrieving serialized objects from file

7 November 2007

I assume that you are aware of how to serialize objects and save them into files on the disk. Now I will talk about how to retrieve serialized objects.

You have to use FileInputStream and ObjectInputStream to fetch the object. Specify the file which contains the object in FileInputStream. Once ObjectInputStrem is initialized, just use in.readObject method of ObjectInputStream to fetch the object.

In the following example, I am fetching a Vector from a file on disk.

FileInputStream f_in = new FileInputStream("C:\\myvector.data");
ObjectInputStream obj_in = new ObjectInputStream (f_in);
Vector v = (Vector)obj_in.readObject();

If file doesn’t exist, java.io.FileNotFoundException will be thrown. We can store and fetch objects of our own classes if they implement Serializable interface. If you want to store multiple objects in same file, you can put them in a Vector and write to a file. Vector class also implement Serializable interface. So can can get vector object back in the same way as shown above.

del.icio.us:Retrieving serialized objects from file  digg:Retrieving serialized objects from file  spurl:Retrieving serialized objects from file  wists:Retrieving serialized objects from file  simpy:Retrieving serialized objects from file  newsvine:Retrieving serialized objects from file  blinklist:Retrieving serialized objects from file  furl:Retrieving serialized objects from file  reddit:Retrieving serialized objects from file  fark:Retrieving serialized objects from file  blogmarks:Retrieving serialized objects from file  Y!:Retrieving serialized objects from file  smarking:Retrieving serialized objects from file  magnolia:Retrieving serialized objects from file  segnalo:Retrieving serialized objects from file  gifttagging:Retrieving serialized objects from file

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Retrieving serialized objects from file

Leave a Reply