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
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