无法将二进制文件中的类对象数组读取到屏幕上。到目前为止,我只能读取第一个对象,尽管文件上有 46 个单独的对象。这是代码:
private void readBinaryFile()
{
ObjectInputStream ois = null;
Person[] personData = null;
try
{
ois = new ObjectInputStream(new FileInputStream(FILE));
personData = (Person[]) ois.readObject();
for(Person p : personData)
{
System.out.println(p.getName() +
"\t" + p.getRanking() +
"\t " + p.getIDNumber());
}
ois.close();
}
catch (FileNotFoundException e) {}
catch (IOException e) {}
catch (ClassNotFoundException e) {}
}
即使代码编译,当我调用该方法时,我也会收到此错误:
java.lang.ClassCastException: Person cannot be cast to [LPerson;