Meaningless Drivel is fun!*
The moose likes I/O and Streams and the fly likes storing and retrieving objects using files, serialization and generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "storing and retrieving objects using files, serialization and generics" Watch "storing and retrieving objects using files, serialization and generics" New topic
Author

storing and retrieving objects using files, serialization and generics

midhuna peru
Ranch Hand

Joined: Aug 24, 2012
Posts: 48
Hi Guys! Iam trying to store and retrieve data using files, serialization and generics. My task is store 5 objects in file and retrieve them. Now After storing the objects in file i donot know how to retrieve all the objects. Here is my code..


The Output is:


How many ever objects i store, When i retrieve, i get the output for the first object that i stored in the file. Why is it so? and how do i retrive all the objects i store in the file?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2165
    
  47
How many ever objects i store, When i retrieve, i get the output for the first object that i stored in the file. Why is it so? and how do i retrive all the objects i store in the file?

You have to call readObject() for each object you want to retrieve. You are just calling it once and so you are only getting the first object.
midhuna peru
Ranch Hand

Joined: Aug 24, 2012
Posts: 48
Oh i got it! Now Say i have added some objects in the File, then how do i retrive all of them without knowing the no of objects in the File ?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2165
    
  47
Well you know how many objects you are going to write so you could start by writing a primitive int value which gives the total number of objects to be written and then write all the objects. Then when you read in the file the first thing you do is read in a primitive int which tells you how many objects you need to read in.
midhuna peru
Ranch Hand

Joined: Aug 24, 2012
Posts: 48
Yah sure ! But what i want to know is instead of keeping a count of objects stored, Is there a way to know the End of File? Like i use hasNext for List....?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2165
    
  47
AFAIK there is no simple test for EOF.
If you try to call readObject when there are no more objects I believe it will throw an OptionalDataException which has a public boolean eof variable which will be set to true. You must test if eof is true because this exception can get thrown for other reasons such as when trying to read an object when the next data is a primitive.
midhuna peru
Ranch Hand

Joined: Aug 24, 2012
Posts: 48
It is throwing java.io.EOFException. how do i test if eof is true here?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2165
    
  47
What version of Java are you using?
The Java docs for 1.6 say "Any attempt to read object data which exceeds the boundaries of the custom data written by the corresponding writeObject method will cause an OptionalDataException to be thrown with an eof field value of true".

Mind you if you are getting an EOFException you don't need to check anything else because this exception is only thrown when the end of the file is reached.
midhuna peru
Ranch Hand

Joined: Aug 24, 2012
Posts: 48
It is 1.6.
This is the when i tried to read the object 3rd time



I have checked everywhere. People say there is no way for this. Guess i must go with the count method !!!
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219

Like I know this is not the most appropriate solution but still.

Store ArrayList's Object into the os.writeObject() method. this ArrayList may contain number of Student objects that you want to write.

At the time of retrieving you will get an ArrayList<StudentList>.
then you easily could get the size of the ArrayList and count how StudentList objects are stored.

thanks


OCPJP
midhuna peru
Ranch Hand

Joined: Aug 24, 2012
Posts: 48
Yah good Idea but ArrayList is temporary. The point is to retrieve data from Permanent storage later ie say another program !!! Maybe a seperate file can be used to keep count of objects whenever added to StudentDetails file everytime.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2165
    
  47
The point Ishan was making was to put the objects in a Collection and then serialize the collection. Then you can de-serialize the serialized ArrayList without having to know how many objects are in it.
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219

Go through through this thread Getting data. you will get something.
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219

And you can definitely do it with the "count" variable as tony suggested.
midhuna peru
Ranch Hand

Joined: Aug 24, 2012
Posts: 48
Got the idea how to catch the exception. Thank you so much guys!

So I removed line 41

and added this instead


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: storing and retrieving objects using files, serialization and generics
 
Similar Threads
Serialization in java
how FileOutputStream and ObjectOutputStream working here
About I/o Files
Nice Serialization Question