I have a class called item
which has a few Strings like price, stock etc.
I write these to a file as such:
item[] data = new item[numberofitems];
The array is then filled then:
ObjectOutputStream outinv = new ObjectOutputStream(outinvstream);
outinv.writeInt(numberofitems);
for (int i = 0; i < numberofitems; i++)
{
outinv.writeObject(data[i]);
}
This all works fine. The trouble comes when I try reading it in:
FileInputStream fin = new FileInputStream(f);
ObjectInputStream ois = new ObjectInputStream(fin);
inventory = (item[]) ois.readObject();
where f is the file (it definitely finds it) and inventory is an item[]
. However I get a OptionalDataException
Any hints are appreciated!
您必须按照与编写顺序相同的顺序进行读取。首先要写入数组大小,因此需要先读取int,然后再读取Objects。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.