Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how can i count objects in an object file  RSS feed

 
rishman toor
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stored so many objects in a file using file output stream.but when read these objects using file input stream just find last one.how can i read all objects in an file?And how can i find that how many objects a file contained?And another question is how can i make a java exe file?
Thankz in advance.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post a minimal expample program demonstrating your problem.
 
rishman toor
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
how can i read all objects from the beginning of a file?How can i get the number of objects in a stored file?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> how can i read all objects from the beginning of a file?

By repeatedly calling readObject

> How can i get the number of objects in a stored file?

By counting the number of times you called readObject.

Have you considered writing one thing to the file, a java.util.List of all your objects?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not too familiar with the Serialization mechanism in Java, but I suspect that each object is the same size. If you can find out this size and the size of the file, you can easily calculate the number of objects by


As for reading every object from the file, you can use a while loop and call readObject() repeatedly, as Jeff suggested. If you just want to read the last object (or any object at a given position), you may want to look at the RandomAccessFile class.

I hope this gives you some suggestions as to what to try. If not, please come back with a description of the problem you are trying to solve. You may also want to post some code to illustrate what you have tried to solve it on your own.

Layne
 
Matt Gatten
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also just make sure your code creates an object with a null value or something last and just keep looping through the read objects while xValue does not equal 'null'.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!