Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in Serialization?

 
lakmal padmakumara
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why have you eaten this exception, just handle it or print it on the console and then run your program.
 
lakmal padmakumara
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did the exact thing !!! that is how i found out that it throws an EOF exception
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:




 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the following: The Finally Block.
 
lakmal padmakumara
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the help from all of you !!! This forum gives a great help to all the Java Learners !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic