Question

So I have a program that is attempting to read in a file to fill in an ArrayList, but keep getting this error when I run the program:

java.io.StreamCorruptedException: invalid stream header: 69652E77

The code:

File saveList = new File("PlayerDatabase.dat");

if(saveList.exists())
{
    FileInputStream FileStream = new FileInputStream(saveList);
    ObjectInputStream ObjStream = new ObjectInputStream(FileStream);

    list = (List<Player>)ObjStream.readObject();
    ObjStream.close();
}
Était-ce utile?

La solution

When you construct an ObjectInputStream, the constructor reads the first two bytes using readStreamHeader() from the stream expecting them to be the magic values that should be present in an object stream.

protected void readStreamHeader()
        throws IOException, StreamCorruptedException
    {
        short s0 = bin.readShort();
        short s1 = bin.readShort();
        if (s0 != STREAM_MAGIC || s1 != STREAM_VERSION) {
            throw new StreamCorruptedException(
                String.format("invalid stream header: %04X%04X", s0, s1));
        }
    }

Thus if it doesn't find either of them it throws the StreamCorruptedException as is evident from the source above. make sure that the file is written in correct format.

Autres conseils

It seems file has not been serialized in correct format, so StreamCorruptedException is thrown while de-serialization.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top