反序列化会抛出StreamCorruptedException

时间:2016-09-04 16:07:16

标签: java serialization

如何正确进行反序列化?为行Movie movie = (Movie) ois.readObject();抛出StreamCorruptedException(:无效的类型代码:AC)。

static void loadMovieFromDataBase(List<Movie> list) {
    try {
        FileInputStream fis = new FileInputStream("movies.ser");
        ObjectInputStream ois = new ObjectInputStream(fis);

        while(fis.available() > 0) {
            Movie movie = (Movie) ois.readObject();
            list.add(movie);
        }

        ois.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    }

序列化:

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("movies.ser", true));
            oos.writeObject(obj);
            oos.close();

0 个答案:

没有答案