• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Reading several objects from a binary file

 
Greenhorn
Posts: 4
Android IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys and girls!

I apologize if I am duplicating an existing topic - I could not find anything similar to what I am trying to do here.
Basically, this is my java final project, and I am having an issue with reading from binary file. I wrote a calendar interface, where a user selects a date that is then passed to an object Ek along with two other Strings. Ek is then written to the binary file "data.dat" (appended to the end of file). A user can then either add more records (Ek) or read records from file. Reading a single object back is fine, but with several objects it only reads the first one and then throws IOException.
I am not sure what I am doing incorrectly.

Could anyone help me with this, please?

Here is the code (run MainMenu) - sorry, it's a huge mess right now:











Thank you!
 
Saloon Keeper
Posts: 8220
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a lot of code to look through. Can you provide a short snip-it that demonstrates your problem?
 
Jessie Staton
Greenhorn
Posts: 4
Android IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I forgot to add that the binary file shows all records and grows every time a new record is added, so I assume that output stream works fine.
 
Jessie Staton
Greenhorn
Posts: 4
Android IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, here is how an object gets written to file:



And here is how it is read back:



It reads the first object fine, then (when looping the second time around), it goes all the way to line 28, and then jumps to line 49.
Please see attached for a sample output I get.

Thank you!

try1.png
[Thumbnail for try1.png]
 
Carey Brown
Saloon Keeper
Posts: 8220
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change this

to

See if that gives you any more insights.
 
Jessie Staton
Greenhorn
Posts: 4
Android IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey,

Thank you so much! I changed the output to print out the exception itself, and found out that input stream gets corrupted on the second object: java.io.StreamCorruptedException: invalid type code: AC

I solved this by creating a new ObjectInputStream inside the while loop:


I also realized that I never actually append new data to my appendto string, so I changed

to be

All records are showing up now.
Once again, thank you so much for your help! I have, probably, spent 5 days trying to figure this out...
 
Carey Brown
Saloon Keeper
Posts: 8220
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad I could help.

FYI:

is the same as
 
Our first order of business must be this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic