實現在SD卡中加載多個對象的代碼:
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();
}
但是此代碼只能加載一個對象。多謝指點。
由readObjects()的說明中提到:
Reads the next object from the source stream 讀取*下一個*對象
我建議用一個循環讀取:
List<Dog> dogs = new ArrayList<Dog>();
Dog dog;
try {
in = new ObjectInputStream(new FileInputStream("/mnt/sdcard/somelocation/save.data"));
while((dog = (Dog) in.readObject()) != null)
dogs.add(dog);
in.close();
}