我有一个简单的类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();
}
}
}
从https://docs.oracle.com/javase/7/docs/api/java/io/ObjectInputStream.html“ObjectInputStream反序列化先前使用ObjectOutputStream编写的原始数据和对象** ”。 – Nomad
你试过这个'FileInputStream fin = new FileInputStream(new File(“bibdefault.txt”))'? – Babel
此外,要知道一个BOM的:http://stackoverflow.com/questions/4897876/reading-utf-8-bom-marker/#4897993 –