If all of the elements of an ArrayList are Serializable,
you can then serialize the list to an ObjectOutputStream
and later read it back from an ObjectInputStream.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.List;
publicclass MainClass {
publicstaticvoid main(String[] a) throws Exception {
List list = Arrays.asList(new String[] { "A", "B", "C", "D" });
FileOutputStream fos = new FileOutputStream("list.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(list);
oos.close();
FileInputStream fis = new FileInputStream("list.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
List anotherList = (List) ois.readObject();
ois.close();
System.out.println(anotherList);
}
}