Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving a class using non serializable objects to file

I have created a class using non serializable objects such as ArrayList's, etc... this class implments Serializable but of course when I try to write a class instance to file using the ObjectOutputStream's writeObject method, I get an IOException.
How to avoid this problem? The writeObject method is final so I can't override it, do I have to save manually (reading all the ArrayList's and saving it's elements one per one) the object, or there is a chance to manage writeObject method to work?

P.S.: I think it's not necessary to include since it's not significative for the problem, but if you think it's necessary to post the code (which also contains other user-defined classes, so it's hard to understand), please let me know.

like image 732
Ramy Al Zuhouri Avatar asked Dec 05 '25 02:12

Ramy Al Zuhouri


1 Answers

  1. You don't need to override or 'provide an implementationofwriteObject()`.

  2. ArrayList is serializable, as per comment above.

  3. You need to read the actual error message you are getting. It's not just 'an IOException', it contains information. Almost certainly it is telling you that some other class isn't serializable. So that's what you have to fix. And doing that doesn't usually involve writeObject() methods either.

like image 157
user207421 Avatar answered Dec 06 '25 14:12

user207421



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!