我有一个简单的类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();
}
}
}
答案 0 :(得分:2)
文件写错了,并没有包含正确的序列化数据。它也没有发生在您声称的行上,因为它无法抛出该异常。