This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes IO / Serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "IO / Serialization" Watch "IO / Serialization" New topic
Author

IO / Serialization

wuan luen hong
Greenhorn

Joined: Aug 23, 2010
Posts: 4
I attempted to insert a data into an ArrayList and write it into a file and then later on pull it out then read and print it out. However, each time I run the program, when I insert the data using scanner class, instead of adding it into the list, it rewrites the entire file instead, displaying only the inserted input. I don't understand what went wrong, as it is supposed to update phoneList and then called it out as another list called newPhoneList and print, but it now only prints whatever is provided in the input. The constructor is as below:


And this part is the main method:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

wuan luen hong wrote:

There's your List - a newly created one. You then add one single element to it, and write it to the file. Of course this file will have a List with only have one element.
What you need to do is read the List from the file instead of creating it each time again. Only if the file does not exist should you create a new List:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
That's what you're doing in your main method:
1) Let the user enter a 'phone' and print it out
2) Serialize a list of 'phone' objects that always contains only one element
3) Recreate a list of 'phone' objects that always contains only one element from serialized file and print it out

That's what you probably want to do:
1) Read previously serialized contents
2) Let user enter new 'phone' object
3) Serialize the list

Don't work with 'phoneList' and 'newPhoneList' ... but always with the same list.


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

It is because everytime you are creating a new file..not checking for existing file and data in it..


SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

beaten 3 minutes by Peter Taucher & Rob Prime
wuan luen hong
Greenhorn

Joined: Aug 23, 2010
Posts: 4
Hi Rob, I can see now what the logical error is. Instead of only creating a new list for the first time, I've repeatedly create a list, replacing the old one each time I run the program. That would explain why it is only displaying one input and not adding on to the list.

Hi,Peter, what you're implying is that, when I insert data into a list, I should also read from the same list? Doesn't that mean that the 2nd part of the code where the newPhoneList takes place is redundant? I've always had the impression that when I perform Serialization, after I insert the data into a prior list, i have to create another new list to read from the first.
wuan luen hong
Greenhorn

Joined: Aug 23, 2010
Posts: 4
Hi again Rob,
Regarding your reply earlier, if the file should exist, the logic would be to immediately grab the list in the existing file instead of creating a new list. I don't however know how to read it as a list again. Is it done using the readObject() method?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

You're already doing it, with newPhoneList.
wuan luen hong
Greenhorn

Joined: Aug 23, 2010
Posts: 4
I tinkered with the code a little and came out with this:


Although now I can input and keep the first information there, I still can't append the next few information into that text file (phoneFile.txt) that I'll be entering in the next few attempts. It gives me an error when i use the method writeObject() (in the commented part towards the end of the code) to update the text file, saying incompatible type or something. Any idea?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Not without seeing the error.

But you are now duplicating much code. Think the following through: what is the only difference between having an existing file and having to create a new List? Is the entire reading of user input part of it? Is the writing of the List at the end part of it?

You should separate your code into three parts:
- the creation / reading of the list
- handling user input
- writing the results back to the file
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IO / Serialization
 
Similar Threads
De-Serializing Objects
Why not its ClassCastException?
Values are assigning null
transient I.0
Customize readObject and writeObject