How do I load multiple objects in Android from SD card?
I got this:
ObjectInput in;
Dog dog = null;
try
{
in = new ObjectInputStream(new FileInputStream("/mnt/sdcard/somelocation/save.data"));
dog = (Dog) in.readObject();
in.close();
} catch (Exception e)
{
e.printStackTrace();
}
but this only loads a singe object from the SD card.
I am thinking something like ArrayList<Dog> dogs = in.readAllObjects(), but this code will only come true in my dreams.
Code samples will be appreciated.
From
readObjects()documentation:So I recommend using a loop to read every dog:
I don’t know if
dogwill ever benullof the top of my head, but it will definitely throw an exception ifintries to read past the end of the file.