简体   繁体   中英

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. 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.

I've gotten the saving function to work however, I can't get the program to read the file. I'm getting an:

error at "cars = in.readObject();".

I've placed my code below, any help would be appreciated. (PS. I can't swap the ArrayList for anything else)

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

cars = (Car)in.readObject();

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM