• Post Reply Bookmark Topic Watch Topic
  • New Topic

ObjectOutputStream exception  RSS feed

 
Alex Barilo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys! I'm kind of greenhorn) and here it is the issue I faced. I can't understand how to read different types of data from file by using ObjectInputStream. Means, I write in file an object and can read it, but if I write ta the same file long primitive it is unable to read it, more over the StreamCorruptedException is thrown. I was trtying to find the answer but nothing. And once else question - why it is impossible to read serilizable object from file in which this object was written up? Thanks in advance.

.......

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new FileOutputStream(fileName, true)

This will definitely cause a problem the second time you run this code - you can't append to a file containing serialized objects.

Otherwise, the code looks good, assuming that "Node" is serializable.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch

I added code tags to your post, and doesn't it look better!
 
Alex Barilo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, guys for reply and for tags (I didn't notice it)). I fixed and it works out! Could you also tell the difference between DataInput/OutputStream and ObjectInput/OutputStream when writing/reading primites - I guess that Data class is more appropriate in such cases but what is the real difference? Thank you in advance.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!