java - 循环反序列化仅执行一次

标签 java

我想将循环对象加载到列表中,但我有一个问题,因为循环仅执行一次,下一个循环会崩溃 IOException 行 'movie = (Movie) ois.readObject () ;'。

@SuppressWarnings("unchecked")
static <T> void loadDatabase(List<T> tab, int index) {
    if(index == 1) {
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("movies.ser"));

            try {
            while(true) {
                Movie movie = new Movie();
                movie = (Movie) ois.readObject();
                tab.add((T) movie);
            }
            } catch(EOFException ignored) {
                ois.close();
            }

        } catch (FileNotFoundException e) {
            System.out.println("Can not find the file. Please try again later...");
        } catch (IOException e) {
            System.out.println("Unable to save file. Please try again later...");
        } catch (ClassNotFoundException e) {
            System.out.println("The error of the projection class Movie");
        }
    } else if(index == 2) {
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("series.ser"));

            while(ois.available() != 0) {
                Series series = new Series();
                series = (Series) ois.readObject();
                tab.add((T) series);
            }
            ois.close();

        } catch (FileNotFoundException e) {
            System.out.println("Can not find the file. Please try again later...");
        } catch (IOException e) {
            System.out.println("Unable to save file. Please try again later...");
        } catch (ClassNotFoundException e) {
            System.out.println("The error of the projection class Series");
        }
    }
}

最佳答案

让我们仔分割析一下你的(抱歉,但是太可怕了)代码:

@SuppressWarnings("unchecked")
static <T> void loadDatabase(List<T> tab, int index) {
  if(index == 1) { 

不要那样做。 index 的全部意义似乎是区分这个方法应该做什么。提示:创建两个方法;并避免该参数和 if!

您可以调用loadMovieFromDataBase()的一个方法;另一个loadSeriesFromDataBase()

    try {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("movies.ser"));
        try {
        while(true) {

你真的想永远循环吗?!这就是 while(true) 所要做的。 好吧,不是永远,而是直到抛出一些异常为止。因此,即使您的循环体正在做正确的事情(事实并非如此!)...该代码也必定会导致某些错误情况。

            Movie movie = new Movie();
            movie = (Movie) ois.readObject();

这简直就是无稽之谈。在 Movie 对象上调用 new 来让 ObjectInputStream 读取它是没有意义的。

Movie movie = (Movie) ois.readObject();

是从 ObjectInputStream 反序列化对象的方式。

            tab.add((T) movie);
        }
        } catch(EOFException ignored) {
            ois.close();

提示:您是初学者。您知道您的代码在做什么;但您有足够的信心忽略异常(exception)情况;甚至没有追踪到他们?!

下面的捕获也好不了多少: } catch (FileNotFoundException e) { System.out.println("找不到该文件,请稍后重试..."); } catch (IOException e) { System.out.println("无法保存文件,请稍后重试...");

您尝试读取文件。提示:复制和粘贴错误消息时要小心。这在这里没有任何意义。除此之外:不要用一些自定义消息替换异常。至少也打印异常消息。因为:你自己的代码都是在撒谎。

    } catch (ClassNotFoundException e) {
        System.out.println("The error of the projection class Movie");
    }

不,上述错误意味着您尝试反序列化一个在该代码运行的 JVM 上下文中不存在的类。

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("series.ser"));

        while(ois.available() != 0) {
            Series series = new Series();
            series = (Series) ois.readObject();

与上面相同的故事。并提示:如果有更多输入,请不要查询流。只需序列化 Series 对象的一个列表即可;而不是稍后反序列化一个对象。

我在这里花时间的原因是:你似乎在盲目地拼凑代码,而丝毫不知道代码在做什么。这是一个坏主意。

您最好退后一步,阅读并运行有关序列化的优秀教程。只有当您能够运行并理解该部分时,您才能尝试编写自己的代码。

关于java - 循环反序列化仅执行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39317529/

相关文章:

java - 无法找到 Spring NamespaceHandler for XML schema 命名空间问题

java - 在 Windows 上配置 JNotify

java - 将 QRCode 转换为图像。 java

Java打印包含多个整数的字符串

java - Apache 米娜 : No error or call back when the endpoint is not accessible

java - GWT:向 CellTable 添加过滤

java - Java(8)中是否有类似于Haskell中的 "let"或 "where"的东西

java - 如何创建一张表的多个映射? (jpa hibernate )

java - 与第三方库解耦

java - 标签库和变量声明在源页面顶部生成空行