Serialization of Arrays of Objects

U

uri

Hi guys. Hopefully this isn't a stupid typo error question, but...

I have my own object

// examp.java
class Security implements Serializable { String a, b; int a; }
...

And I have an array of instances of that object (which I'm not sure I'm
creating correctly.


Security allStocks[] = new Security[2]


I write the array like thus:

try
{
ObjectOutputStream out = new ObjectOutputStream(new
FileOutputStream("Securities.dat"));
out.writeObject(allStocks);
out.close();
}
catch (IOException e)
{
System.out.println(e);
}

Then, I read it like this:

try
{
ObjectInputStream in = new ObjectInputStream(new
FileInputStream("Securities.dat"));
allStocks = (Security) in.readObject();
in.close();
}
catch (IOException e)
{
System.out.println(e);
}


And I get "incompatible types" compile error on the line that says
allStocks = (Security) in.readObject(); ... the two lines are in two
different classes, how does it even know what type of object is being
read? I don't understand. Is it not possible to serialize and save an
array of objects?

Thanks in advance!

(Sorry about the code, it's hand-typed.)

--Uri
 
V

Vova Reznik

Security allStocks[] = new Security[2]


FileInputStream("Securities.dat"));
allStocks = (Security) in.readObject();
in.close();
}
catch (IOException e)
{
System.out.println(e);
}

How about
allStocks = (Security[]) in.readObject();
 
B

Boudewijn Dijkstra

uri said:
Hi guys. Hopefully this isn't a stupid typo error question, but...

I have my own object

// examp.java
class Security implements Serializable { String a, b; int a; }
..

And I have an array of instances of that object (which I'm not sure I'm
creating correctly.


Security allStocks[] = new Security[2]

Please separate the type and the name:

Security[] allStocks = new Security[2];
I write the array like thus:

try
{
ObjectOutputStream out = new ObjectOutputStream(new
FileOutputStream("Securities.dat"));
out.writeObject(allStocks);
out.close();
}
catch (IOException e)
{
System.out.println(e);
}

Then, I read it like this:

try
{
ObjectInputStream in = new ObjectInputStream(new
FileInputStream("Securities.dat"));
allStocks = (Security) in.readObject();

....because it avoids confusion in these cases.
in.close();
}
catch (IOException e)
{
System.out.println(e);
}


And I get "incompatible types" compile error on the line that says
allStocks = (Security) in.readObject(); ... the two lines are in two
different classes, how does it even know what type of object is being
read?

Because you got the error from the compiler.
I don't understand. Is it not possible to serialize and save an
array of objects?

Only with compatible types.
 
B

Bjorn Abelli

...

[snip]
Security[] allStocks = new Security[2]
[snip]

allStocks = (Security) in.readObject();
[snip]

And I get "incompatible types" compile error on the line
that says allStocks = (Security) in.readObject(); ...
the two lines are in two different classes, how does it even
know what type of object is being read?

It doesn't, but it knows the type of the variable "allStocks", which is
*not* Security, but an *array* of Security, and *that's* what the compiler
is complaining about.

// Bjorn A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top