[英]Can't read object information from a binary file, Java
I'm writing a program that saves information about a car within an object ArrayList.我正在编写一个程序,将有关汽车的信息保存在 object ArrayList 中。 I want to be able save the information from the ArrayList into a binary file, then load and view that information at a later date.
我希望能够将 ArrayList 中的信息保存到二进制文件中,然后加载并在以后查看该信息。
I've gotten the saving function to work however, I can't get the program to read the file.我已经得到了保存 function 的工作但是,我无法让程序读取文件。 I'm getting an:
我得到一个:
error at "cars = in.readObject();".
“汽车 = in.readObject();”处的错误。
I've placed my code below, any help would be appreciated.我已将我的代码放在下面,任何帮助将不胜感激。 (PS. I can't swap the ArrayList for anything else)
(PS。我不能将 ArrayList 换成其他任何东西)
ArrayList<carClass> cars = new ArrayList<carClass>();
public void openFile(){
cars = null;
try {
FileInputStream fileIn = new FileInputStream("yareyare.ora");
ObjectInputStream in = new ObjectInputStream(fileIn);
cars = () in.readObject();
in.close();
fileIn.close();
}
catch (IOException i) {
i.printStackTrace();
return;
}
}
Here's what i'm adding to the array if you are interested如果您有兴趣,这就是我要添加到数组中的内容
System.out.println("What is the make of the car?");
String newMake = scan.next();
System.out.println("What is the model of the car?");
String newModel = scan.next();
System.out.println("What year was the car produced?");
String newYear = scan.next();
System.out.println("How far has this car traveled?");
String newOdometer = scan.next();
cars.add(new Car(newMake, newModel, newYear, newOdometer));
You must convert your object into a car object您必须将您的 object 转换为汽车 object
cars = (Car)in.readObject();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.