gpt4 book ai didi

java - 反序列化对象 java 列表 -> INVALID STREAM HEADER

转载 作者:行者123 更新时间:2023-12-02 04:39:47 25 4
gpt4 key购买 nike

我有一个简单的类Ksiazka,并尝试序列化和反序列化它的列表。首先,我需要从文件“bibdefaout.txt”加载它。我不断收到错误:

Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: 5AB36F64
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at zadanie.Zadanie.main(Zadanie.java:17)

请告诉我我做错了什么:

public class Ksiazka implements Serializable{
protected String tytul;
protected String autor;
protected Integer rok;
protected boolean wypozyczenie;

public Ksiazka(String tytul, String autor, Integer rok, boolean wypozyczenie) {
this.tytul = tytul;
this.autor = autor;
this.rok = rok;
this.wypozyczenie = wypozyczenie;
}
}

public class Zadanie {
public static void main(String[] args)
throws FileNotFoundException,IOException, ClassNotFoundException {
List<Ksiazka> lista;
// THE FOLLOWING LINE PRODUCES AN ERROR:
FileInputStream fin=new FileInputStream("bibdefault.txt");
ObjectInputStream oin=new ObjectInputStream(fin);
lista=(List<Ksiazka>)oin.readObject();
fin.close();
oin.close();

try {
ObjectOutputStream out=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("bibloteka.out")));
out.writeObject(daneLista);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

最佳答案

文件写入错误并且不包含正确的序列化数据。它也没有发生在您声明的行上,因为它不能抛出该异常。

关于java - 反序列化对象 java 列表 -> INVALID STREAM HEADER,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30297091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com