reg. StreamCorruptedException
Hi,
Can U help me How 2 solve "StreamCorruptedException" problem.
This exception i'm getting when i'm trying 2 use readObject().
First time it is giving EOFException().from next time onwards it is giving StreamCorruptedException().
code is:
import java.io.*;
public class FileTry1
{
// instance variables - replace the example below with your own
private UserRec currUser;
/**
* Constructor for objects of class FileTry1
*/
public FileTry1()
{
// initialise instance variables
UserRec u1 = new UserRec(0, "John3", 1000);
UserRec u2 = new UserRec(1, "joe3", 2000);
UserRec u3 = new UserRec(100, "naren", 2000);
try {
FileOutputStream ostream = new FileOutputStream("t.tmp", true);
ObjectOutputStream p = new ObjectOutputStream(ostream);
p.writeObject(u1);
p.writeObject(u2);
p.writeObject(u3);
p.flush();
ostream.close();
p.close();
}
catch (Exception e) {
System.out.println(e);
} // end catch
ReadIt ri = new ReadIt();
}
public static void main(String args[] ) {
new FileTry1();
System.exit(0);
}
} // end class FileTry1
import java.io.*;
public class UserRec implements Serializable
{
// instance variables - replace the example below with your own
public int userID;
public String userName;
public int salary;
/**
* Constructor for objects of class UserRec
*/
public UserRec()
{
// initialise instance variables
userID = 0;
userName = "";
salary = 0;
}
public UserRec(int id, String name, int sal) {
// overridden constructor w/ values
userID = id;
userName = name;
salary = sal;
} // end constructor...
}
-
import java.io.*;
public class ReadIt
{
// instance variables - replace the example below with your own
private UserRec urec;
/**
* Constructor for objects of class ReadIt
*/
public ReadIt()
{
// initialise instance variables
urec = new UserRec();
FileInputStream istream = null;
ObjectInputStream p = null;
// now try to read the file we just wrote
try {
istream = new FileInputStream("t.tmp");
p = new ObjectInputStream(istream);
System.out.println("Now going to try to read the file back in");
boolean EOF = false;
int count=0;
while ( !EOF ) {
try {
UserRec t_urec = (UserRec) p.readObject();
System.out.println("rec No.: " + t_urec.userID + " Name: " + t_urec.userName);
if(++count == 4) break;
} // end try
catch (Exception f) {
EOF = true;
System.out.println("in ReadIt at EOF catch: " + f);
f.printStackTrace();
} // end catch EOF
} // end while ( !EOF )
istream.close();
p.close();
} // end try
catch (Exception ee) {
System.out.println("Overall ReadIt catch: " +ee);
}
} // end ReadIt()
} // end class ReadIt
Thanks in advance
Naren.

