如何正确进行反序列化?为行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();