How to Serialize and Deserialize Array List in Java?

Ads
 

How to Serialize and Deserialize Array List in Java?

In this article we are going to write programs in Java for Serialize and Deserialize Array List. Our program will save Array List object to a file and then read the file to construct Array List object.

In this article we are going to write programs in Java for Serialize and Deserialize Array List. Our program will save Array List object to a file and then read the file to construct Array List object.

Java Serialization: How to Serialize and Deserialize Array List in Java?

In this tutorial we are going to explain you the Serialization and Deserialization concept in Java with an example code. In this example code we are going to create an object of List, add some values and then serialize the state to a file. In another we will read the serialized file into List object and print the values stored in the list. This way you can use the serialization of Java to save the state of any serializable object to a file. Later on the serialized file can be read into the object of same type to construct the state of the object. In Java Serialization and deserialization is used to send the object over network.

What is Serialization and Deserialization in Java?

The Serialization and Deserialization  are two very import concept of Java. Every Java developer must learn to use these concepts very well. The Serialization in Java refers the mechanism of converting the state of an object into byte stream which can be further transferred be transferred or saved for further retrieved. The Deserialization concept refers to the mechanism of converting the byte stream back to object. Through the Deserialization feature of Java you can read back the previously Serialized stream to respective Java object.

Java Serialization: How to Serialize and Deserialize Array List in Java?

Example of serializing ArrayList in Java

In the first example we are going to create a simple Java program that will serialize the object of ArrayList into a file. Here we are creating an object of Java ArrayList and then adding few values to it. Then with the help of following code it is saved to the file:

FileOutputStream fos = new FileOutputStream("./list.out");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(lst);

Here is Java program code to save Array List object to a file:


package my.app;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.*;

public class JavaSerializeListSaveExample {

	public static void main(String[] args) {
		List lst = new ArrayList();
		lst.add("One");
		lst.add("Two");
		lst.add("Three");
		lst.add("Four");
		lst.add("Five");
		lst.add("Six");
		lst.add("Seven");
		lst.add("Eight");
		lst.add("Nine");
		lst.add("Ten");

		try {
			FileOutputStream fos = new FileOutputStream("./list.out");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(lst);
			oos.close();
			fos.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
		
		System.out.println("Written lst data to list.out file.");

	}

}

Reading serialized ArrayList from file into List object

In the above example code we have saved the state of List into a file and with the help of following code we are going to read into List object:

FileInputStream fis = new FileInputStream("./list.out");
ObjectInputStream ois = new ObjectInputStream(fis);
lst = (ArrayList) ois.readObject()

Array list to construct the object:


package my.app;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.*;

public class JavaSerializeListReadExample {

	public static void main(String[] args) {
		List lst = new ArrayList();
		

		try {
            FileInputStream fis = new FileInputStream("./list.out");
            ObjectInputStream ois = new ObjectInputStream(fis);
            lst = (ArrayList) ois.readObject();
            ois.close();
            fis.close();
            
            //Print list data
            for(String str: lst){
                System.out.println(str);
            }            
            
		} catch (Exception ioe) {
			ioe.printStackTrace();
		}

	}

}

Here is more example of Java Serialization:

Ads

Ads