문제

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();
}
도움이 되었습니까?

해결책

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.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top