The Artima Developer Community
Sponsored Link

Java Answers Forum
Problem with read and write Collection

6 replies on 1 page. Most recent reply: Jan 3, 2004 8:34 AM by Joseph Lee

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Joseph Lee

Posts: 10
Nickname: at9483mph
Registered: Jan, 2004

Problem with read and write Collection Posted: Jan 1, 2004 12:45 PM
Reply to this message Reply
Advertisement
I am a student doing my final project, I need help urgently. Can anyone please advise me?

I am not sure how I can apply write and read for a collection. I have written it out to a File through an ObjectOutputStream out with out.writeObject(arraylist). That seems to work and the file is there. When I attempt to read it back with arraylist.add(in.readObject()),I can't get anything. Actually I need to save all my objects into a file then retrieve it back later by doing so.

I am not sure am I doing the correct way? or anyone please advise/guide me for a better way?

Thank you.


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Problem with read and write Collection Posted: Jan 1, 2004 5:31 PM
Reply to this message Reply
Are your objects inside array list implementing java.io.Serializable interface?

Joseph Lee

Posts: 10
Nickname: at9483mph
Registered: Jan, 2004

Re: Problem with read and write Collection Posted: Jan 1, 2004 10:41 PM
Reply to this message Reply
> Are your objects inside array list implementing
> java.io.Serializable interface?

No, i am not using serializable because i don't know I know what is it...I just added all my Object into Arraylist.

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Problem with read and write Collection Posted: Jan 2, 2004 3:15 AM
Reply to this message Reply
>>>>
I have written it out to a File through an ObjectOutputStream out with out.writeObject(arraylist).

since u have written an arraylist, u will be getting object of arraylist when u read it...(a single object)

read objects from this arraylist (not sure if u can read from it)

give it a try...

try ArrayList al = (ArrayList )(in.readObject());

and hope for the best... :)

Joseph Lee

Posts: 10
Nickname: at9483mph
Registered: Jan, 2004

Re: Problem with read and write Collection Posted: Jan 2, 2004 5:51 AM
Reply to this message Reply
I have tried to use serialization and it works but the objects saved and loaded seem not the same ( I get it from System.out.println ) Could anyone please tell me why? The other problem is I couldn't get my arraylist get repaint??

If anyone please advise me? THANK YOU VERY MUCH!!

Below are parts of my coding ::


public void saveArray()
{
try
{
FileOutputStream out = new FileOutputStream("t.tmp");
ObjectOutputStream objOutput =
new ObjectOutputStream(out);
Devices[] d =
(Devices[])devicesList.toArray(new Devices[0]);
for (int i=0; i<d.length; i++)
{
System.out.println(d);
}
objOutput.writeObject(d);
objOutput.flush();
objOutput.close();
}
catch(Exception e)
{
e.printStackTrace();
}


public void getArray()
{
try
{
FileInputStream in = new FileInputStream("t.tmp");
ObjectInputStream objInput = new ObjectInputStream(in);
Devices[] d = (Devices[])objInput.readObject();
for (int i=0; i<d.length; i++)
{
devicesList.add(d);
}
p.readObject();
in.close();
}
catch(Exception e)
{
e.printStackTrace();
}

for (int i = 0; i < devicesList.size(); i++)
{
System.out.println(devicesList.get(i));
}
repaint();
validate();
}

Ronald Tetsuo Miura

Posts: 22
Nickname: ronaldtm
Registered: Jan, 2004

Re: Problem with read and write Collection Posted: Jan 2, 2004 6:22 AM
Reply to this message Reply
What is the output of this code? Does it throw any exception (like NullPointerException or ClassCastException)? Does it print the objects, just that they aren't the same?

When loading serialized objects, the comparison a==b doesn't work, because they aren't the same object. Serialization doesn't maintain object identity, just load its contents back, keep this in mind.

Because of that, unless you override tne device.toString() method to print its fields' values, it will print different strings (the Object class default implementation returns somewhat like a pointer).

If the implementation of Device contains any transient field, or any of its field's objects contains it, it will not be serialized, and when you load it, they will be set to null.

And, make Device implement the interface Serializable. It doesn't contain any abstract method, so you don't need to add any code besides implements Serializable, but it marks the class as serializable.

What is p.readObject(); intended to do?
Well, it doesn't matter, anyway, but the code you posted won't compile, because p is not declared, unless it is a object field (p as a field name? BAAAD practice!) =)

And, just a remark, you aren't serializing a Collection, but just an array of Devices (you coded it right, anyway)

Joseph Lee

Posts: 10
Nickname: at9483mph
Registered: Jan, 2004

Re: Problem with read and write Collection Posted: Jan 3, 2004 8:34 AM
Reply to this message Reply
Thanks Ronald. I have serialized the NetworkDevices, it is the correct way?( as below ). It do not throw any Exception but printed it ( just they aren't the same )

What I am going to do is all my objects properties are added to the collection ( devicesList ). Everytime I repaint the canvas, I will get it back from the the collection where I have a method named "drawDevice" in every class like Workstation. So, I am trying to save the collection to file and get it back from file then repaint.Is it the correct way? or any better way suggested?

Below are some of my classes, and the coding posted is in DragAndDrop class.

THANK YOU!!!

My classes:

i) public interface Devices extends Cloneable

ii) public abstract class NetworkDevices implements Devices ,Serializable

iii) public class Workstation extends NetworkDevices
( I have a numbers of classes like Workstation )

p/s: p.readObject(); --> (I have cut this line of coding, typing error, sorry)

Flat View: This topic has 6 replies on 1 page
Topic: Question about Jframe Previous Topic   Next Topic Topic: \u014F  \301

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use