0

Previously, I have wrote a arraylist data into a binary file called (ItStateBinary.dat)

and now I am trying to read the arraylist from the binary file, then assign each of the element in arraylist to the array.

so far i have this:

public CarOwner[] readListFromBinary() throws Exception
{
    String fileName = "ItStateBinary.dat";
    FileInputStream inStream = new FileInputStream(fileName);
    ObjectInputStream objectInputFile = new ObjectInputStream(inStream);

    //need to create CarOwner[] object called temp and return
}

readListFromBinary() method Reads an ArrayList collection from a binary file (ltStateBinary.dat). Then, each ArrayList object item is then written to a newly created CarOwner[] called temp. temp is returned to the calling method.

edit:

 public CarOwner[] readListFromBinary() throws Exception
{
    String fileName = "ItStateBinary.dat";
    FileInputStream inStream = new FileInputStream(fileName);
    ObjectInputStream objectInputFile = new ObjectInputStream(inStream);

    ArrayList<CarOwner> read = (ArrayList<CarOwner>)objectInputFile.readObject();

    CarOwner[] temp = read.toArray(new CarOwner[read.size()]); 
    return temp;
}

does anybody know whats wrong with this method? it gives me compiler warning

1 Answer 1

1

I'm not sure what you asking, but assuming that you write your ArrayList to the file like that:

ArrayList<CarOwner> al = new ArrayList<CarOwner>();
FileOutputStream fos = new FileOutputStream("ItStateBinary.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(al);

That way, you can just read the way, your are already doing:

FileInputStream fis = new FileInputStream("ItStateBinary.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList<CarOwner> read = (ArrayList<CarOwner>)ois.readObject();

Then just return you return the array from your ArrayList:

return read.toArray(new CarOwner[read.size()]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.