Java objectInputStream 返回 java class
Java objectInputStream back to java class
我是 java 的新手,这只是我的学习代码。我遇到了问题。我已经将一组人发送到文件中,
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.ser"))) {
for (Person person : persons) {
oos.writeObject(person);
}
} catch (IOException ex) {
Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
}
现在我只想让两个最重要的人回到新的人 Class 数组(愚蠢,但我只是想学习如何使用文件)并且实际上不知道如何操作。
我试过这样的东西
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.ser")))
{
Person[] humans = new Person[2];
humans[0] = new (Person) ois.readObject();
humans[1] = new (Person) ois.readObject();
}
catch (IOException ex) {
Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
}
但是没用。有人可以帮忙吗?我觉得只是有点小错误。
替换
humans[0] = new (Person) ois.readObject();
humans[1] = new (Person) ois.readObject();
有
humans[0] = (Person) ois.readObject();
humans[1] = (Person) ois.readObject();
我是 java 的新手,这只是我的学习代码。我遇到了问题。我已经将一组人发送到文件中,
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.ser"))) {
for (Person person : persons) {
oos.writeObject(person);
}
} catch (IOException ex) {
Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
}
现在我只想让两个最重要的人回到新的人 Class 数组(愚蠢,但我只是想学习如何使用文件)并且实际上不知道如何操作。 我试过这样的东西
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.ser")))
{
Person[] humans = new Person[2];
humans[0] = new (Person) ois.readObject();
humans[1] = new (Person) ois.readObject();
}
catch (IOException ex) {
Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
}
但是没用。有人可以帮忙吗?我觉得只是有点小错误。
替换
humans[0] = new (Person) ois.readObject();
humans[1] = new (Person) ois.readObject();
有
humans[0] = (Person) ois.readObject();
humans[1] = (Person) ois.readObject();