• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

ObjectOutputStream -- Error.

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
}
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephen,

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



Regards
 
reply
    Bookmark Topic Watch Topic
  • New Topic