0

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();
        }
    }
}
Peter Lang
  • 54,264
  • 27
  • 148
  • 161
aczajkow
  • 21
  • 3
  • From https://docs.oracle.com/javase/7/docs/api/java/io/ObjectInputStream.html "An ObjectInputStream deserializes **primitive data and objects** previously written using an ObjectOutputStream." – almeynman May 18 '15 at 07:19
  • Have you tried this `FileInputStream fin=new FileInputStream(new File("bibdefault.txt"))` ? – Rookie007 May 18 '15 at 07:27
  • Also, be aware of a BOM: http://stackoverflow.com/questions/4897876/reading-utf-8-bom-marker/#4897993 – Arthur Eirich May 18 '15 at 07:45

1 Answers1

2

The file is written wrong and doesn't contain proper serialized data. It's also not happening on the line you claim, since it can't throw that exception.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • sorry its the next line ,the one ObjectInputStream oin=new ObjectInputStream(fin); and the file looks like that:Złodziejka książek;Zusak Markus;2014 Sezon burz;Sapkowski Andrzej;2013 Akademia pana Kleksa;Brzechwa Jan;2010 Lśnienie;Stephen King;2011 i can't get what is wrong with it – aczajkow May 18 '15 at 07:20
  • 1
    Serialized data is not text. Read a tutorial about serialization before you continue. – Kayaman May 18 '15 at 07:22