This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ObjectOutputStream -- Error.  RSS feed

 
Sujatha Kumar
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using objectoutputstream to read/write the contents of an array
to the file.

I dont get any problem if i read the file but i am encountering
the following error when i try to read from the file.

Reading from ObjectOuputStream
java.lang.ClassCastException: [LPatient;
at Ward.main(Ward.java:368)

Any help is appreciated.

Following are the two class files that i have been using.

public class Patient implements Serializable
{
private String name = null;

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}
}


public class Ward
{
public static void main(String args[]) throws Exception
{

try
{
Patient[] patients = new Patient[1];
patients[0] = new Patient();

//Writing to File
patients[0].setName("Ravi");
File file = new File("myfile");
FileOutputStream fOS = new FileOutputStream (file);
ObjectOutputStream oOS = new ObjectOutputStream (fOS);
oOS.writeObject (patients);

//Reading From File
File myFile = new File("myfile");
FileInputStream fIS = new FileInputStream (myFile);
ObjectInputStream oIS = new ObjectInputStream(fIS);
patients[0] = (Patient) oIS.readObject();

}
catch(Exception e)
{
e.printStackTrace();
}
}
}
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You write an array, and read it as single Object.
And code-tags wouldn't make your code less readable.
You may add them now.
[ August 22, 2004: Message edited by: Stefan Wagner ]
 
Sujatha Kumar
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have added the code tags now, Could explain me why i am getting this
problem.

It would be great if you could give me the solution.

Regards
 
Sujatha Kumar
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephen,

Thanks for you reply, I got it right now by making the following
changes



Regards
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!