I have the following code:
ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream("test.dat"));
ArrayList<String> list = null;
for(int i = 0; i < 10; i++)
{
list = new ArrayList<String>();
list.add("Object" + i);
oo.writeObject(list);
}
oo.close();
When I open the test.dat file and the unserializeobjects, I get all the objects. But if I change my code to this:
ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream("test.dat"));
ArrayList<String> list = new ArrayList<String>();
for(int i = 0; i < 10; i++)
{
list.clear();
list.add("Object" + i);
oo.writeObject(list);
}
oo.close();
Now, when I read objects, I get only the first, that is, Object0. Can someone explain the behavior?
source
share