java.io.StreamCorruptedException
at java.i
bjectInputStream.readObject0(ObjectInputStream.java:1326)
at java.i
bjectInputStream.readObject(ObjectInputStream.java:339)
at fileTools.FileTools.getUserDefListFromFile(FileTools.java:303)
at plotImages.SimChooser.initLists(SimChooser.java:99)
See
http://mindprod.com/jgloss/gotchas.html#SERIALIZATION
and
http://mindprod.com/jgloss/runerrormessages.html#STREAMCORRUPTEDEXCEPTION
I wrote myself a little SSCCE to investigate appending. I generated
it with the file I/O Amanuensis to help avoid clerical errors.
See
http://mindprod.com/applets/fileio.html
// test ability to append to a serialized object stream
import java.io.*;
public class ObjAppend
{
/**
* write some string objects to serialized file
*/
private static void write1() throws IOException
{
// O P E N
FileOutputStream fos = new FileOutputStream( "C:/temp/temp.ser",
false /* append */ );
BufferedOutputStream bos = new BufferedOutputStream( fos, 4096
/* buffsize */ );
ObjectOutputStream oos = new ObjectOutputStream( bos );
// W R I T E
oos.writeObject( "a string" );
oos.writeObject( "a second string" );
oos.reset();
oos.writeObject( "a third string" );
oos.flush();
// C L O S E
oos.close();
}
/**
* Append a string object to serialized file
*/
private static void write2() throws IOException
{
// O P E N
FileOutputStream fos = new FileOutputStream( "C:/temp/temp.ser",
true /* append */ );
BufferedOutputStream bos = new BufferedOutputStream( fos, 4096
/* buffsize */ );
ObjectOutputStream oos = new ObjectOutputStream( bos );
// W R I T E
oos.writeObject( "a fourth string, appended" );
oos.flush();
// C L O S E
oos.close();
}
/**
* read back four strings from serialized file
*/
private static void read1() throws IOException,
ClassNotFoundException
{
// O P E N
FileInputStream fis = new FileInputStream( "C:/temp/temp.ser" );
BufferedInputStream bis = new BufferedInputStream( fis, 4096 /*
buffsize */ );
ObjectInputStream ois = new ObjectInputStream( bis );
// R E A D four strings
for ( int i=0; i<4; i++ )
{
System.out.println( (String) ois.readObject() );
}
// C L O S E
ois.close();
}
/**
* test ability to append to a serialiszed object stream
*
* @param args not used
*/
public static void main ( String[] args )
{
try
{
write1();
write2();
read1();
}
catch ( Exception e )
{
e.printStackTrace();
}
} // end main
}
---------------------------------
It dies with the following, output just like yours reading the
appended string.
a string
a second string
a third string
java.io.StreamCorruptedException
at java.i
bjectInputStream.readObject0(Unknown Source)
at java.i
bjectInputStream.readObject(Unknown Source)
at ObjAppend.read1(ObjAppend.java:63)
at ObjAppend.main(ObjAppend.java:80)
The key to why this fails can be found by examining the file
c:\temp\temp.ser with a hex viewer. See
http://mindprod.com/jgloss/hex.html
In short, what gets written to the file on an append is not the same
as when you do a reset. readObject is not smart enough to ignore
extra stream init stuff in the middle of a stream.
So the short answer is, you can't append. You must write to a new
file, or copy and write to a new file.