This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

END OF STREAM problem  RSS feed

 
Kaushik Mehta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!