wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Problem in Serialization? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem in Serialization?" Watch "Problem in Serialization?" New topic
Author

Problem in Serialization?

lakmal padmakumara
Ranch Hand

Joined: Aug 08, 2009
Posts: 47
There is a ArrayList which contains user objects .And I serialize those objects to a file called "Users.txt".



But I think my serializing isn't successful .When I open that file I can see a collection of squares .I think In serializing you can see some letters also in the serialized file .But here only squares .

When I try to read the serialized file useing



It throws EOF Exception .Which means there are not any objects in my file .Please can someone figure out the error???

[Bsc in Computer Science and Engineering] [SCJP 80%][BCS certificate level]
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


Why have you eaten this exception, just handle it or print it on the console and then run your program.


SCJP 6
lakmal padmakumara
Ranch Hand

Joined: Aug 08, 2009
Posts: 47
I did the exact thing !!! that is how i found out that it throws an EOF exception
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
lakmal padmakumara wrote:I did the exact thing !!! that is how i found out that it throws an EOF exception


Is User class implements Serializable?
Put your complete code here.
lakmal padmakumara
Ranch Hand

Joined: Aug 08, 2009
Posts: 47
Thanks for Your speedy reply !!Yr it is !!It has implemented serializable


This is where I select a file and ask to serialize!
if userRadioButton selected it calls savetoFile() method in user class!
here is my User Class



after these operations when i checked that "Users.txt" file it has some square signs .I don't think it has serialized successfully !! please give me a help !!!

Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
Hi,

I didn't check your last post ... the code is just "too long'.

Regarding the EOFException.
This is expected behavior in your case.
Why?
The reason is following code:
while ((user=(User)osi.readObject()) != null) {
In order to avoid EOFException, you'll have to store a size of the list, which would then be read while restoring.

That's the beauty of a binary file.
Everything has to be stored in order to be successfully restored (of course in same sequence). ;)


Regards,
Rok


SCJP, SCWCD
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
You create a new OutputStream and close it with each iteration through the loop. Move the declaration to the outside of the loop and then write all the User objects:





Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Your code is working and EOFException will come, as said by Rok, you can apply solution suggested by him, or explicitly handle EOFException as success for reading.
lakmal padmakumara
Ranch Hand

Joined: Aug 08, 2009
Posts: 47
Oh Thanks a lot Mr Garrett ,Actually 1 Thank wont be enough .!! Thanks again !! It solved my problem .But now I have another problem about that word "finally" .As I'm a beginner to JAVA I haven't had the chance to work with that key word ! What does it do ?? Can you Explain ???
Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
One more thing.

Garrett Rowe wrote:Move the declaration to the outside of the loop and then write all the User objects:

or use FileOutputStream(file, true); // for appending the data

lakmal padmakumara wrote:after these operations when i checked that "Users.txt" file it has some square signs .I don't think it has serialized successfully !! please give me a help !!!

The serialization works as it should ... the reason is that you're working with a binary format.


Regards,
Rok
Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
lakmal padmakumara wrote:Oh Thanks a lot Mr Garrett ,Actually 1 Thank wont be enough .!! Thanks again !! It solved my problem .But now I have another problem about that word "finally" .As I'm a beginner to JAVA I haven't had the chance to work with that key word ! What does it do ?? Can you Explain ???

It guaranties, that the code in the finally block will always be executed, no matter what.
Usually it is used for cleanup, stream closings, etc.


Regards,
Rok
Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
Read the following: The Finally Block.
lakmal padmakumara
Ranch Hand

Joined: Aug 08, 2009
Posts: 47
Thanks a lot for the help from all of you !!! This forum gives a great help to all the Java Learners !!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem in Serialization?