The Artima Developer Community
Sponsored Link

Java Answers Forum
reg. StreamCorruptedException

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Narender

Posts: 1
Nickname: narenj
Registered: May, 2004

reg. StreamCorruptedException Posted: May 10, 2004 5:10 AM
Reply to this message Reply
Advertisement
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.

Topic: Download a JPG from URL to local file Previous Topic   Next Topic Topic: how to get the ip in domain name server

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use