簡體   English   中英

Java:序列化和反序列化ArrayList

[英]Java: Serialize & DeSerialize ArrayList

我有一個類Book()Author()和一個CollectionOfBooks()類(在這里我將所有書籍都存放在ArrayList中)。 然后,我在界面上有一個菜單,可以在其中添加/列出/刪除/搜索書籍。 一切正常。 但是,我也想在退出程序時將書籍保存在文件中,所以當程序結束時,我調用此方法( BooksIO()是serialize& 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();
    }

我不確定自己在做什么錯,有人可以幫助我嗎? 序列化和反序列化的類:

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

                    }
                }
            }
        }

在outputFile()方法的頂部,您正在將“庫”變量初始化為新的(可能是空的)CollectionOfBooks,然后對其進行序列化。

您要做的是將應用程序的CollectionOfBooks實例傳遞到outputFile()方法,並對其進行序列化。

另外,盡管其他人可能不同意,但我發現Java序列化有些笨拙,並且您需要注意一些奇怪的情況。 除非出於某種原因,我個人不會使用它-我會使用JSON序列化庫,例如GSon。

只是猜測,因為您的問題尚不包含有效的MCVE程序,但您似乎正在閱讀這些書,但未將它們帶到主GUI的用戶界面中,很可能是UserInterface類(我們不,因為您尚未顯示我們)。 如果是這樣的話...

給UserInterface類提供一個public void updateBooks(CollectionOfBooks books)方法,並使用更新的book集合調用此方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM