1

I have saved an arrayList into a binary file by using serialastion. How do I now retrieve this data from the binary file?

This is the code I have used for serialisation

public void createSerialisable() throws IOException
{
    FileOutputStream fileOut =  new FileOutputStream("theBkup.ser");
    ObjectOutputStream out =  new ObjectOutputStream(fileOut);
    out.writeObject(allDeps);
    options();
}

and this the code I am trying to use for deserialization of the arrayList:

public void readInSerialisable() throws IOException
{
    FileInputStream fileIn = new FileInputStream("theBKup.ser");

    ObjectInputStream in = new ObjectInputStream(fileIn);

    try
    {
     ArrayList readob  = (ArrayList)oi.readObject();  
               allDeps = (ArrayList) in.readObject();
    }
    catch (IOException exc)
    {
        System.out.println("didnt work");
    }
}

allDeps is the declared array list in the classes constructer. Im trying to save the arrayList from the file to the arrayList declared in this class.

3
  • of departments which has a name and a location Commented Oct 30, 2011 at 4:29
  • yeah the line with oi should not actually be their. I was just using that line to know how to write the line below it. Just do away with that line. Sorry for the confusion Commented Oct 30, 2011 at 4:33
  • see my answer here: stackoverflow.com/questions/6683582/… Commented Oct 30, 2011 at 4:37

1 Answer 1

3

Your code is mostly correct, but there is one mistake and a couple of things that might make it work better. I've highlighted them with asterisks (since, apparently, I can't make them bold in 'code' mode).

public void createSerialisable() throws IOException
{
    FileOutputStream fileOut =  new FileOutputStream("theBkup.ser");
    ObjectOutputStream out =  new ObjectOutputStream(fileOut);
    out.writeObject(allDeps);
    **out.flush();** // Probably not strictly necessary, but a good idea nonetheless
    **out.close();** // Probably not strictly necessary, but a good idea nonetheless
    options();
}

public void readInSerialisable() throws IOException
{
    FileInputStream fileIn = new FileInputStream("theBKup.ser");

    ObjectInputStream in = new ObjectInputStream(fileIn);

    try
    { 
        **// You only wrote one object, so only try to read one object back.**
        allDeps = (ArrayList) in.readObject();
    }
    catch (IOException exc)
    {
        System.out.println("didnt work");
        **exc.printStackTrace();** // Very useful for findout out exactly what went wrong.
    }
}

Hope that helps. If you still see a problem then make sure you post the stack trace and a complete, self-contained, compilable example that demonstrates the problem.

Note that I've assumed that allDeps contains objects that are actually Serializable and that your problem is in readInSerialisable rather than in createSerialisable. Again, a stack trace would be extremely helpful.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot Cameron, but.. it still will not compile, "its saying unreported exception ClassNotFound must be caught or declared to be thrown" On line : allDeps1 = (ArrayList)in.readObject();
Oh. You didn't mention that it was a compile error. That's easy: add a catch (ClassNotFoundException e) block after the try block. Or just change the catch (IOException exc) to catch (Exception exc).

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.