Hi All
I have created a class student whose 2 objects will be serialized to MyStudent.txt file .Problem is when I am trying to read both record it is giving java.io.StreamCorruptedException.
Can any body help with the solution
Thank in adv
Sandeep
import java.io.*;
class student implements Serializable
{
String name,address;
int age,marks;
public void getdata(String n,String a,int age1,int m)
{
name=n;
address=a;
age=age1;
marks=m;
}
public String getname()
{
return name;
}
public String getaddress()
{
return address;
}
public int getage()
{
return age;
}
public int getmarks()
{
return marks;
}
}
class writerecords
{
public void writerecord(student s)
{
try{
FileOutputStream f=new FileOutputStream("Student.txt",true);
ObjectOutputStream o=new ObjectOutputStream(f);
o.writeObject(s);
f.close();
o.close();
}catch(Exception e){System.out.println("There is a error in writerecord");}
}
public void readrecord()
{
try{
student s;
FileInputStream f=new FileInputStream("Student.txt");
ObjectInputStream o=new ObjectInputStream(f);
s=(student)o.readObject();
System.out.println(s.getname()+"\t"+s.getaddress()+"\n");
//read the second record
s=(student)o.readObject();
System.out.println(s.getname()+"\t"+s.getaddress()+"\n");\
o.close
}
catch(EOFException e)
{
System.out.println("End of File Reached");
System.exit(1);
}
catch(Exception ef)
{
System.out.println("Error");
}
}
public static void main(String args[])
{
student stud=new student();
stud.getdata("Mr x","India",23,56);
writerecords w=new writerecords();
w.writerecord(stud);
//writing the second record
stud.getdata("Mr Y","Bangladesh",46,54);
w.writerecord(stud);
w.readrecord();
}
}
Exception
java.io.StreamCorruptedException: Type code out of range, is -84
at java.io.ObjectInputStream.peekCode(ObjectInputStream.java:1280)
at java.io.ObjectInputStream.refill(ObjectInputStream.java:1401)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:273)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:232)
at writerecords.readrecord(writerecords.java:56)
at writerecords.main(writerecords.java:80)