ArrayList is serializable by default. This means you need not to implement Serializable interface explicitly in order to serialize an ArrayList. In this tutorial we will learn how to serialize and de-serialize an ArrayList.
Example:
Serialization: Run the below class and it will create a file myfile which will be having ArrayList object in form of Stream of bytes. We would be using “myfile” at the receivers end to recreate the Object from stream of bytes. Note: We did not implemented the Serializable interface in the below class because ArrayList is already been serialized by default.
package beginnersbook.com; import java.util.ArrayList; import java.io.*; public class ArrayListSerialization { public static void main(String [] args) { ArrayList<String> al=new ArrayList<String>(); al.add("Hello"); al.add("Hi"); al.add("Howdy"); try{ FileOutputStream fos= new FileOutputStream("myfile"); ObjectOutputStream oos= new ObjectOutputStream(fos); oos.writeObject(al); oos.close(); fos.close(); }catch(IOException ioe){ ioe.printStackTrace(); } } }
De-Serialization:
In this class we are retrieving the stream of bytes from myfile which we have stored using the above class. We are type casting the returned object to ArrayList and displaying the elements of ArrayList. Observe the output: We are getting the same elements which we have added to the ArrayList before the serialization.
package beginnersbook.com; import java.io.*; import java.util.ArrayList; public class DeSerializationClass { public static void main(String [] args) { ArrayList<String> arraylist= new ArrayList<String>(); try { FileInputStream fis = new FileInputStream("myfile"); ObjectInputStream ois = new ObjectInputStream(fis); arraylist = (ArrayList) ois.readObject(); ois.close(); fis.close(); }catch(IOException ioe){ ioe.printStackTrace(); return; }catch(ClassNotFoundException c){ System.out.println("Class not found"); c.printStackTrace(); return; } for(String tmp: arraylist){ System.out.println(tmp); } } }
Output:
Hello Hi Howdy
That’s all I have for the topic serialization of ArrayList. Please let me know if you have any questions.
craig says
Very helpful code and straight to the point. thanks.
craig says
the line : arraylist = (ArrayList)ois.readObject(); produces an unchecked conversion warning !!
Nitin says
java.io.StreamCorruptedException: invalid type code: 0A
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1374)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at java.util.ArrayList.readObject(ArrayList.java:733)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1004)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1866)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
These Errors are coming from the reading line.
Sujit says
It is working fine
Hema says
What if we have a combination of Strings and Integer in the file when processing for de-serialization. As we declared the ArrayList arraylist as String.
Asrinivas says
I am getting below exeception for the same
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.arrays.DeSerializationClass.main(DeSerializationClass.java:17)
Akshay says
how it will be done in case of arraylist of non-primitive data types(array list of an object)