Java:序列化和反序列化ArrayList

时间:2018-10-12 01:12:37

标签: java arrays serialization deserialization

我有一个类Book()Author()和一个类CollectionOfBooks()(我将所有书籍都存放在ArrayList中)。然后,我在界面上有一个菜单,可以在其中添加/列出/删除/搜索书籍。一切正常。但是,我也想在退出程序时将书籍保存在文件中,以便在程序结束时调用此方法(BooksIO()是serialize&deserialize的类):

public void exitProgram() throws IOException{
        System.out.println("Program shuts down, cya!");
        BooksIO.outputFile(); // to save my books to the file
    }

我不确定这些书是否已保存,因为启动程序时这些书不会显示:

public static void main(String[] args) throws IOException, ClassNotFoundException {
        UserInterface menu = new UserInterface();
        BooksIO.inputFile(); // get the books from the saved file to the library
        menu.run();
    }

我不确定自己在做什么错,有人可以帮助我吗? 序列化和反序列化的类:

    public class BooksIO {

            public static  void outputFile() throws IOException{
                CollectionOfBooks library = new CollectionOfBooks(); //where the books are saved in an ArrayList
                FileOutputStream fout=null;
                ObjectOutputStream oos=null;
                try{
                    fout = new FileOutputStream ("stefi.ser");
                    oos=new ObjectOutputStream(fout);
                    // I try to save my library to the file
                    oos.writeObject(library.Books); 

                    System.out.println("Serializing successfully completed");

                    for(Book c: library.Books){
                        System.out.println(c.toString());
                    }
                } catch (IOException ex){
                    System.out.println(ex);
                }finally{
                    try{
                        if(fout!=null) fout.close();
                        if(oos!=null) oos.close();
                    } catch (IOException e){

                    }
                }
            }

            public static void inputFile() throws IOException, ClassNotFoundException{

                CollectionOfBooks library = new//where my books are saved in an ArrayList of type Book CollectionOfBooks();//where my books are saved in an ArrayList of type Book
                ObjectInputStream ois = null;
                try{
                    FileInputStream fin = new FileInputStream("stefi.ser");
                    ois = new ObjectInputStream(fin);
                    // try to get my books from the file and save it in the library
                    library.Books  = (ArrayList<Book>)ois.readObject();
                    System.out.println("Deserializing successfully completed");

                    for(Book c: library.Books){
                        System.out.println(c.toString());
                    }

                }catch (ClassNotFoundException e){
                    System.out.println("The class for this type of objects"+
                            "does not exist in this application!");
                    throw e;
                }finally{
                    try{
                        if(ois!=null){
                            ois.close();
                        }
                    }catch (IOException e){

                    }
                }
            }
        }

2 个答案:

答案 0 :(得分:3)

就在outputFile()方法顶部,您正在将“库”变量初始化为新的(可能是空的)CollectionOfBooks,然后对其进行序列化。

您要做的是将应用程序的CollectionOfBooks实例传递到outputFile()方法,并对其进行序列化。

另外,尽管其他人可能不同意,但我发现Java序列化有点笨拙,并且您需要注意一些奇怪的情况。除非出于某种原因,我个人不会使用它-我会使用JSON序列化库,例如GSon。

答案 1 :(得分:1)

只是猜测,因为您的问题尚不包括有效的MCVE程序,但是您似乎在看书,但没有到达主GUI的目录,很可能是UserInterface类(我们不知道)不是,因为您没有向我们展示。如果是这样...

为UserInterface类提供一个public void updateBooks(CollectionOfBooks books)方法,并通过更新后的书集进行调用:

public void updateBooks(CollectionOfBooks collectionOfBooks) {
    this.collectionOfBooks = collectionOfBooks;
}

更改inputFile以返回CollectionOfBooks对象:

public static CollectionOfBooks inputFile() throws IOException, ClassNotFoundException{
    //...

}

,然后返回集合。

然后主要可以这样做:

public static void main(String[] args) throws IOException, ClassNotFoundException {
    UserInterface menu = new UserInterface();
    menu.updateBooks(BooksIO.inputFile()); 
    menu.run();
}