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.

J. Doe
  • 29
  • 7
Binyomin
  • 367
  • 2
  • 7
  • 20
  • of departments which has a name and a location – Binyomin Oct 30 '11 at 04: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 – Binyomin Oct 30 '11 at 04:33
  • see my answer here: http://stackoverflow.com/questions/6683582/best-way-to-store-data-for-your-game-images-maps-and-such/6683609#6683609 – Eng.Fouad Oct 30 '11 at 04:37

1 Answers1

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.

Cameron Skinner
  • 51,692
  • 2
  • 65
  • 86
  • 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(); – Binyomin Oct 30 '11 at 04:40
  • 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)`. – Cameron Skinner Oct 30 '11 at 05:03