aspose file tools*
The moose likes I/O and Streams and the fly likes problem with the StreamCorruptedException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "problem with the StreamCorruptedException" Watch "problem with the StreamCorruptedException" New topic
Author

problem with the StreamCorruptedException

Naveed Yahya
Greenhorn

Joined: Jan 27, 2003
Posts: 3
Hi, I'm getting StreamCorruptedException in my readObject() function. For the first time I'm able to serialize the objects(two objects of Medicines class) to the file and also able to deserialize those objects successfully.
On subsequent runs I'm again able to deserialize those previously stored objects, but if I add any new Object, and try to read all objects from file then I can only read the previously stored objects successfully(that were stored on the first run) but not the last one.
while reading the last object it throws StreamCorruptedException.
here is the code:
================== Test Class =================
import java.io.*;
public class TestM
{
public static void main(String args[]) throws Exception
{
write();
read();
}

public static void write() throws Exception
{
File myFile = new File("med.dat");
FileOutputStream inFile = new FileOutputStream(myFile,true);
ObjectOutputStream OIS = new ObjectOutputStream(inFile);
Medicines objMed1 = new Medicines(1,"aa",1.1,32);
Medicines objMed2 = new Medicines(2,"bb",2.2,42);
OIS.writeObject(objMed1);
OIS.writeObject(objMed2);
OIS.close();
inFile.close();
}

public static void read() throws Exception
{
File myFile = new File("med.dat");
FileInputStream inFile = new FileInputStream(myFile);
ObjectInputStream OIS = new ObjectInputStream(inFile);
try
{
for(int ctr=0;;ctr++)
{
System.out.println("run : " + ctr);
Medicines objMed = new Medicines();
objMed = (Medicines)OIS.readObject();
System.out.println(objMed);
}
}

catch(EOFException e)
{
System.out.println("EOF Caught");
OIS.close();
inFile.close();
}
}
}

================ Medicines Class ================
import java.io.*;
public class Medicines implements Serializable
{
private int RecNo;
private String name;
private double price;
private long quatity;

public Medicines()
{
this.RecNo = 0;
this.name = null;
this.price = 0;
this.quatity = 0;
}
public Medicines(int Rec, String name, double price, long qty)
{
this.RecNo = Rec;
this.name = name;
this.price = price;
this.quatity = qty;
}

public int getRecNo()
{
return this.RecNo;
}
public String getName()
{
return this.name;
}

public double getPrice()
{
return this.price;
}
public long getQty()
{
return this.quatity;
}

public String toString()
{
return this.RecNo
+ "\t" + this.name
+ "\t" + this.price
+ "\t" + this.quatity;
}
}
================================================
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with the StreamCorruptedException