Java:Serialize和DeSerialize ArrayList

问题描述 投票:-1回答:2

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

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

我不确定我做错了什么,有人能帮帮我吗? Serialize和DeSerialize的类:

    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){

                    }
                }
            }
        }
java arrays serialization deserialization
2个回答
3
投票

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

你想要做的是将应用程序的CollectionOfBooks实例传递给outputFile()方法,并将其序列化。

此外,虽然其他人可能不同意,但我发现Java序列化有点笨拙,并且它有一些你需要注意的奇怪边缘情况。我个人不会使用它,除非我出于某种原因 - 我使用JSON序列化库,比如GSon,而不是。


1
投票

只是猜测,因为你的问题还没有包含一个有效的MCVE程序,但看起来你正在读书而不是把它们带到它们所属的地方,主GUI,很可能是UserInterface类(我们不是没有) ,因为你没有给我们看过)。如果是这样...

为UserInterface类提供一个public void updateBooks(CollectionOfBooks books)方法,并使用更新的book集合调用它:

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

更改输入文件以返回Collection Of Books对象:

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();
}
© www.soinside.com 2019 - 2024. All rights reserved.