• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem when appending to a file of serialized objects

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the problem.
I have a log class which is responsible for recording things to a file and when asked, reading them back.
In a unit test for this class I sent 10 log entries (a small class containing a date and a string) into the log. These were serialized through an ObjectOutputStream into a new file. Then I asked for the objects back and using an ObjectInputStream the objects were all read back properly.
I then ran the same test again, however this time the file already existed so I opened the initial FileWriter to append to the file. I wrote another 10 objects to the file and then attempted to read the file back using the ObjectInputStream. The stream successfully read the 10 objects I put in the first time, but then threw the EOFException after reading only one of the second 10 entries I appended to the file. I looked at the file, and even in serialized form it is obvious all 20 objects are in the file.
If anyone can shed some light as to why this would happen, it would be appreciated. Also, if you need more info about the code, let me know.
Thanks in advance
Rob Cranston
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob, please take a look at our user name policy and re-register with a first and last name. Thanks. As for your question- I'm perplexed by your statment "I opened the initial FileWriter to append to the file". Did you close the original FileWriter and open a new one? (In which case it's not the original FileWriter.) Or do you still have the original FileWriter, which was never closed? (In which case it would be silly to reopen it.)
Hmmm... try opening and closing the ObjectOutputStream without wrinting any actual objects to it, and look at the file. If there's anything at all there, it means that the OOS does something extra when it's opened or closed, and this confuses the reader later. It may be that the OOS is not designed to do what you want, and you'll have to either (a) keep a single OOS until you're definitely done writing all the objects you need to, or (b) rewrite the file from scratch each time. Meaning if you've closed the file and now you have more objects, you must first read the previous objects from the file back into memory (if they aren't already there), then open a FileOutputStream with append=false, then write all the old objects, then write the new objects, and then close the file. Whew!
 
A wop bop a lu bob a womp bam boom. Tutti frutti ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic