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();
        }
    }
}
3

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
Serialized data is not text. Read a tutorial about serialization before you continue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.