• Post Reply Bookmark Topic Watch Topic
  • New Topic

StreamCorrupted Exception  RSS feed

Sridevi Sangaiah
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I have one exception that is breaking my head for the past three days. It�s with streams.
I am writing a serializable object to a file and reading it back. When I open the file in read/write write some objects and read it back, the program is working fine. But when I do the same in append mode and run the program twice I am getting �StreamCorrupted Exception�.
The Structure of the Object I am writing is
package FirstSwings;
import java.io.*;
public class AccountInfo implements java.io.Serializable {
private String self_First_Name;
private String self_Middle_Name;
private String self_Last_Name;
private String self_Dob;
private int shared_Account = 0;
private String share1_First_Name;
private String share1_Last_Name;
private String share1_Dob;
private String share2_First_Name;
private String share2_Last_Name;
private String share2_Dob;
private int account_Type;
private int account_Number;
/** Creates a new instance of AccountInfo */
public AccountInfo() {
Except for these fields I have some setter and getter methods.
I am writing and reading the objects in the following program
package FirstSwings;
import java.io.*;
import FirstSwings.AccountInfo;
public class MySample {
/** Creates a new instance of MySample */
public MySample() {
* @param args the command line arguments
public static void main(String[] args) throws Exception {
FileOutputStream fos= new FileOutputStream(AccountInfoConstants.FILE_NAME);
//try out opening the file in append mode and run the program twice.
ObjectOutputStream out = new ObjectOutputStream(fos);
AccountInfo info = new AccountInfo();
info.setPersonalInfo(1," self_First_Name1", "self_Middle_Name"," self_Last_Name", "self_Dob", 1);
AccountInfo info1 = new AccountInfo();
info1.setPersonalInfo(2," self_First_Name2", "self_Middle_Name"," self_Last_Name", "self_Dob", 1);
AccountInfo info2 = new AccountInfo();
info2.setPersonalInfo(3," self_First_Name3", "self_Middle_Name"," self_Last_Name", "self_Dob", 1);


FileInputStream fis = new FileInputStream(AccountInfoConstants.FILE_NAME);
ObjectInputStream ois = new ObjectInputStream(fis);
FirstSwings.AccountInfo account_Info = null;
account_Info = info;

account_Info = (FirstSwings.AccountInfo)ois.readObject();
System.out.println("first time :"+account_Info.getAccountNumber());
System.out.println("first time :"+account_Info.getFirstName());
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing an ObjectOutputStream writes are a protocol identification number (magic number) and protocol version number. If you write to the same file with different ObjectOutputStreams, you'll get copies of that data in the middle of the file -- where they are disallowed. Perhaps writing a subclass of ObjectOutputStream and overriding the method writeStreamHeader() to do nothing would do the trick. I gather from the javadocs that all header writing is done from within that method. You would then have to override the readStreamHeader() method in a sublcass of ObjectInputStream to do nothing.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!