反序列化对象java的列表->;无效的流标头
我有一个简单的类反序列化对象java的列表->;无效的流标头,java,serialization,Java,Serialization,我有一个简单的类Ksiazka,并尝试序列化和反序列化它的列表。首先,我需要从文件“bibdefaout.txt”中加载它。我一直收到一个错误: Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: 5AB36F64 at java.io.ObjectInputStream.readStreamHeader(Unknown Source) at java.io.Obj
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)
公共类扎达尼{
公共静态void main(字符串[]args)
抛出FileNotFoundException、IOException、ClassNotFoundException{
清单a;
//以下行产生错误:
FileInputStream fin=新的FileInputStream(“bibdefault.txt”);
ObjectInputStream oin=新ObjectInputStream(fin);
lista=(List)oin.readObject();
fin.close();
oin.close();
试一试{
ObjectOutputStream out=新的ObjectOutputStream(新的BufferedOutputStream(新的FileOutputStream(“bibloteka.out”)));
out.writeObject(daneLista);
out.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
文件写入错误,不包含正确的序列化数据。这也不会发生在您声明的行上,因为它不能引发该异常。来自“ObjectInputStream反序列化以前使用ObjectOutputStream编写的基本数据和对象”。您是否尝试过此FileInputStream fin=new FileInputStream(新文件(“bibdefault.txt”)
,注意BOM:抱歉,这是下一行,ObjectInputStream oin=新ObjectInputStream(fin);文件看起来是这样的:Złodziejka książek;祖萨克马库斯;2014年塞松布尔;萨普科夫斯基·安德泽伊;2013年Akademia pana Kleksa;Brzechwa Jan;2010年Lśnienie;斯蒂芬·金;2011年,我无法理解它的错误,序列化数据不是文本。继续之前,请阅读有关序列化的教程。
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();
}
}
}