Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:序列化&;反序列化ArrayList_Java_Arrays_Serialization_Deserialization - Fatal编程技术网

Java:序列化&;反序列化ArrayList

Java:序列化&;反序列化ArrayList,java,arrays,serialization,deserialization,Java,Arrays,Serialization,Deserialization,我有一个类Book(),Author()和一个类CollectionOfBooks()(我把所有的书都放在一个ArrayList中)。然后我有了我的界面,在那里我有一个菜单,我可以添加/列出/删除/搜索书籍。一切正常。但是,我还想在退出程序时将书籍保存在一个文件中,以便在程序结束时调用此方法(BooksIO()是用于序列化和反序列化的类): 我不确定这些书是否保存,因为当我启动程序时,这些书不会显示: public static void main(String[] args) throws I

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

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

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

                    }
                }
            }
        }
公共类图书{
公共静态void outputFile()引发IOException{
CollectionOfBooks library=new CollectionOfBooks();//将书籍保存在ArrayList中的位置
FileOutputStream fout=null;
ObjectOutputStream oos=null;
试一试{
fout=新文件输出流(“stefi.ser”);
oos=新对象输出流(fout);
//我尝试将我的库保存到文件中
oos.writeObject(图书馆、书籍);
System.out.println(“序列化成功完成”);
(c册:图书馆,图书){
System.out.println(c.toString());
}
}捕获(IOEX异常){
系统输出打印项次(ex);
}最后{
试一试{
如果(fout!=null)fout.close();
如果(oos!=null)oos.close();
}捕获(IOE异常){
}
}
}
公共静态void inputFile()引发IOException,ClassNotFoundException{
CollectionOfBooks library=new//我的书保存在Book CollectionOfBooks()类型的ArrayList中;//我的书保存在Book类型的ArrayList中
ObjectInputStream ois=null;
试一试{
FileInputStream fin=新的FileInputStream(“stefi.ser”);
ois=新对象输入流(fin);
//试着从文件中取出我的书并保存在图书馆
library.Books=(ArrayList)ois.readObject();
System.out.println(“反序列化成功完成”);
(c册:图书馆,图书){
System.out.println(c.toString());
}
}catch(classnotfounde异常){
System.out.println(“此类型对象的类”+
“此应用程序中不存在!”);
投掷e;
}最后{
试一试{
如果(ois!=null){
ois.close();
}
}捕获(IOE异常){
}
}
}
}

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

相反,您要做的是将应用程序的CollectionOfBooks实例传递到outputFile()方法中,并将其序列化


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

只是猜测,因为你的问题还没有包含有效的MCVE程序,但看起来你在书中阅读,但没有找到它们所属的地方,主GUI,很可能是UserInterface类(我们没有,因为您没有向我们展示)。如果是的话

给UserInterface类a
public void updatebook(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();
}

你看到什么例外了吗?正在创建文件吗?更新?在您的主方法中,我看不到您将任何存储的书籍加载到UserInterface对象的位置。你在哪里做这件事?@HovercraftFullOfEels我的主要任务是:BooksIO.inputFile();事实上,看起来您在
inputFile()
方法中创建了一个CollectionOfBooks对象,但是这个对象与主GUI有什么关系呢?我猜您在那里创建了一个单独的CollectionOfBooks对象,一个与在
inputFile()
中创建的对象完全无关的对象。如果是真的,这将不起作用——您必须更新可视化对象的状态。此外,当我运行/关闭程序时,它会“成功”打印出
“此外,尽管其他人可能不同意,但我发现Java序列化有点笨拙,……我个人不会使用它”
——我想我们大多数人都会同意。1+
public static void main(String[] args) throws IOException, ClassNotFoundException {
    UserInterface menu = new UserInterface();
    menu.updateBooks(BooksIO.inputFile()); 
    menu.run();
}