This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Java in General and the fly likes END OF STREAM problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "END OF STREAM problem" Watch "END OF STREAM problem" New topic


Kaushik Mehta

Joined: Feb 10, 2003
Posts: 4
Iam storing objects in a file using ObjectOutputStream(FileOutputStream()). Let us say n objects. When i read the objects,since i do not know how many objects are there , i need to use a loop(while or anything else). How will i know that end of stream has been reached? I tried -1 that does not work because iam reading objects. And when i try for null also it gives an exception.
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Can think of two things offhand:
  • Stuff your objects in a List or array, then serialize the list. Presto, you know exactly how many objects to deserialize: exactly one
  • Create a class called EndOfFile; as the very last object in your stream, serialize an EndOfFile.
  • There may be a much simpler way though. Anyone?
    - Peter
    I agree. Here's the link:
    subject: END OF STREAM problem
    jQuery in Action, 3rd edition