我在反序列化 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();
numitems
是用户想要制作对象的次数。问题来自阅读,我目前有:
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);
}
但我不知道如何从那里继续前进。