循环反序列化只执行一次

时间:2016-09-04 13:40:37

标签: 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");
        }
    }
}

2 个答案:

答案 0 :(得分:1)

让我们解析一下你的(对不起,但可怕,可怕)代码:

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

你真的想要永远循环 吗?这就是(真实)的意思。 好吧,不是永远,而是直到抛出一些异常。所以,即使你的循环体正在做正确的事情(它不是!)......那些代码必须导致一些错误状态。

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

提示:你是初学者。您有没有想法代码正在做什么;但你有足够的信心忽略例外;甚至没有追踪他们?!

以下捕获量并没有好多少:         } catch(FileNotFoundException e){             System.out.println(&#34;找不到文件。请稍后再试......&#34;);         } catch(IOException e){             System.out.println(&#34;无法保存文件。请稍后再试......&#34;);

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

    } 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对象;而不是稍后反序列化一个对象。

我之所以在这里度过的原因:看起来你是盲目地将代码放在一起而没有任何想法代码正在做什么。那是一个坏主意。

你最好退一步阅读&amp;运行有关序列化的好教程。只有当您能够运行并了解该部分时,您才会尝试使用自己的代码。

答案 1 :(得分:0)

实际上这是个坏主意。最好的是反序列化一次,克隆并添加列出你想要多少时间。

相关问题