Java 如何反序列化已创建的2个对象?

Java 如何反序列化已创建的2个对象?,java,serialization,arraylist,inputstream,deserialization,Java,Serialization,Arraylist,Inputstream,Deserialization,我在反序列化2个对象时遇到问题。这两个对象来自同一个类,但参数不同。 以下是我编写的代码: /* Write */ FileOutputStream out = new FileOutputStream(inventoryList); ObjectOutputStream writeinvL = new ObjectOutputStream(out); ArrayList<File> Read = new ArrayLi

我在反序列化2个对象时遇到问题。这两个对象来自同一个类,但参数不同。 以下是我编写的代码:

        /* Write */
        FileOutputStream out = new FileOutputStream(inventoryList);
        ObjectOutputStream writeinvL = new ObjectOutputStream(out);
        ArrayList<File> Read = new ArrayList<File>();
        for (int i = 0; i < numItems; i++) {
            System.out.print("Enter the item number: ");
            itemNum = input.next();
            System.out.print("Enter the item name: ");
            itemName = input.next();
            System.out.print("Enter the amount in stock: ");
            inStock = input.nextInt();
            writeinvL.writeObject(new Item(itemNum, itemName, inStock));
        }
        writeinvL.close();

但是我不知道如何从那里继续。

在第二段代码中,在for循环中,您将对该项反序列化两次。也就是说,有两行包含readIn.readObject。每个对象只能执行一次

替换:

array.addItem readIn.readObject


array.addinv

你要把inv放在哪里?对不起,我现在可能有点健忘,有点扯头发哈哈。好的,很好。我编辑了答案以澄清问题,但听起来你现在已经知道了。PS:如果你能接受我的答案,那就是我帮助你的报酬!哇,当然。再次感谢,这是我第一次使用Stackoverflow,所以我不知道答案按钮:P lol。这个网站非常有用!
        ArrayList<Item> array = new ArrayList<Item>();
        FileInputStream in = new FileInputStream(inventoryList);
        ObjectInputStream readIn = new ObjectInputStream(in);

        for (int i = 0; i < numItems; i++) {
            Item inv = (Item) readIn.readObject();
            array.add((Item) readIn.readObject());
        }
        readIn.close();
        for (Item n : array) {
            System.out.println(n);
        }