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.
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).
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
allDepscontains objects that are actuallySerializableand that your problem is inreadInSerialisablerather than increateSerialisable. Again, a stack trace would be extremely helpful.