Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

storing and retrieving objects using files, serialization and generics

 
midhuna peru
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2969
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2969
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2969
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is throwing java.io.EOFException. how do i test if eof is true here?
 
Tony Docherty
Bartender
Posts: 2969
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
midhuna peru
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2969
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through through this thread Getting data. you will get something.
 
Ishan Pandya
Ranch Hand
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you can definitely do it with the "count" variable as tony suggested.
 
midhuna peru
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got the idea how to catch the exception. Thank you so much guys!

So I removed line 41

and added this instead


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic