I have a simple class Ksiazka
and try to serialize and deserialize a list of it. Firstly I need to load it from the file "bibdefaout.txt". I keep getting an error:
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)
Please tell me what am I doing wrong:
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();
}
}
}