• 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

Reading unknown number of objects from InputObjectStream

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

I'm on my way to certify (i gave myself two weeks but I think i have to reconsider but am reading as fast as I can..

I am currently reading about ObjectInputStream/OutputStreams. I have saved a file with 10 products (products, see below for class) and I am able to retrieve them without a hitch. No worries there.

However, what I wanted to do, is to retrieve a unknown number of Products from a file. For example I write 10 Products, then append another 10 products to the file, making it 20 products in the file.

When I try to deserialize the objects, it will read the first 10 without a problem, but will encounter a unknown value when it reaches 10.

Anyone know why?

Here is a sample of the code, for the readObjects method.




Product is just a simple class, implementing the Serializable interface. No mumbo jumpbo here.







edit: Added static main() and public void writeObjects().
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Everything looks fine to me with the code. After reading 10 objects, you'll get an EOFException (EndOfFileException) marking that there are no more objects to read...
 
Oddbjorn Lona
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, well, thats the problem, i have written more than 10 objects. The thing i have written them in two "passes". say i have a method that writes the 10 objects. Then i call the method again, with append to the file. SO, there should be 20 objects.

Now, if i try to read that file, the list will still stop on 10 objects ....
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show us the code to write the objects

And welcome to javaranch .

[edit]

Also you might want to know about serialVersionUID...
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:Can you show us the code to write the objects



Yes, indeed. My guess is that you haven't written 20 objects. My guess is that you wrote 10 objects, then wrote the same 10 objects over again. But the code will tell.
 
Oddbjorn Lona
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the welcome

Darn it, did not bring the code with back home. Basically i did call the method that wrote to the file, twice, and i can see that the file increases in size.

Basically you're saying there is nothing wrong with the notion, of writing to the file twice (and appending) and you should be able to deserialize the entire file like i have done ?

o.
 
Oddbjorn Lona
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I added the code for main() and writeObjects().

The code has been modified since my first post, but the general idea is the same, and i still run into the same problem.

As you can see from the main method, i call the writeObjects twice, to replicate saving to a file more than once, or for that matter. Still working on it :-)

edit: seems like i am getting an error like this: java.io.StreamCorruptedException: invalid type code: AC. The file appears to be locked...
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still can't tell from your code whether the second 10 objects are different objects than the first 10 objects. It does make a difference.
 
Oddbjorn Lona
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am posting the whole shebang ...
There a 4 files.

ObjectSeries which is the writing and reading of the objects, also including the main method that runs the whole example.

Then, I've got Product.java and ProductLine. Product has-a (several) ProductLines (5 to be exact).

TestData.java is just a class that generates up the array of Product and Productline.

Please not, that i have modifed objectSeries, thinking it was because i was writing the 10 same objects, but now i am sure they are not the same anymore. Still get the error ...













 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you can just concatenate two files like that. Try using the same ObjectOutputStream for all objects.
 
Oddbjorn Lona
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:I don't think you can just concatenate two files like that. Try using the same ObjectOutputStream for all objects.



Seems that way. The deserializing works fine, once i do not append the file, so i guess that must be the answer....

Thanks!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic