Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with ObjectInputStream

 
Sandeep Ghosh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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)
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I made you something for you to understand how this all works. I have redesigned a little bit the classes in order to get something more robust...
One thing though, the code below works only for one Student, if you want to write more of them, you have to keep track of the file pointer and read subsequent object from there (I let you this as an exercise )
Here is the code, in two separate file (Serializer.java and Student.java), just put them in the same directory and run javac Serializer.java and then java Serializer and this should work.



Ok let me know if you have trouble understanding the code...
HIH
Val
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sandeep Ghosh:

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)

To make this code work without error
You must not append to the file when you write
AND Comment the section below
***********************
//read the second record
s=(student)o.readObject();
System.out.println(s.getname()+"\t"+s.getaddress()+"\n");\
**********************
If you observe this code
It seems that two different records aka objects are written to a file...ie "India" first and then "Bangladesh" second which is assumed that its being appended
But in reality thats not what happening,
calling
FileOutputStream f=new FileOutputStream("Student.txt",true);
ObjectOutputStream o=new ObjectOutputStream(f);
this above code twice DOES NOT APPEND instead it CORRUPTS
To append to the file, use RandomAccessFile
and move filepointer to read and write

Let me know if it helps
Ragu

 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rigth Ragu, so Sandeep remove the "true" literal in the instantiation of the FileOutputStream in writeToFile...
Val
 
Sandeep Ghosh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ragu and Valentin
Thanks a lot for help,but I still I have one question left.Why we use true in ObjectOutputStream constructor
Sandeep
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sandeep Ghosh:
Hi Ragu and Valentin
Thanks a lot for help,but I still I have one question left.Why we use true in ObjectOutputStream constructor
Sandeep

Hello Sandeep...
ObjectOutputStream do not have a constructor with an append mode
The "true" you mentioned is to ensure that the written datas can be appended to the existing file.
Normally appending is a low-level stream mechanism ie like File

Hope it helps you
Ragu
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic