Serialize and Deserialize an ArrayList in Java

In this post, you will learn how to Serialize and Deserialize an ArrayList in Java using the FileOutputStream and FileInputStream classes.

How to Serialize an ArrayList in Java?

Serialization is the process of changing the state of an object into a byte stream. For example, we use it to write some Java objects into a file.

In this post, you will learn how to Serialize an ArrayList in Java.

ArrayList is serializable by default, so we don’t need to do anything else except writing the code that will serialize the List into a byte stream.

Example:

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

class Test {

  public static void main(String[] args) {
    ArrayList<String> cities = new ArrayList<>();

    cities.add("Paris");
    cities.add("Rome");
    cities.add("London");
    cities.add("Tokyo");

    try {
      FileOutputStream outputStream = new FileOutputStream("list_of_cities");
      ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);

      objectOutputStream.writeObject(cities);
      objectOutputStream.close();
      outputStream.close();
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }
}


You should see the file “list_of_cities” saved into your project root folder.

Serialize an ArrayList of custom objects

Below is the program that serializes an ArrayList of Person objects. In this case, the Person class must implement the Serializable interface. Otherwise, we will get the NotSerializableException.

Example

import java.io.*;
import java.util.*;

class Person implements Serializable {

  private String name;
  private int age;

 // constructors, getters, setters and toString() method
}

class Test {

  public static void main(String[] args) {
    ArrayList<Person> persons = new ArrayList<>();

    persons.add(new Person("John", 32));
    persons.add(new Person("Alek", 31));
    persons.add(new Person("Megan", 27));
    persons.add(new Person("Melissa", 25));

    try {
      FileOutputStream outputStream = new FileOutputStream("list_of_persons");
      ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);

      objectOutputStream .writeObject(persons);
      objectOutputStream .close();
      outputStream.close();
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }
}

How to Deserialize an ArrayList in Java?

Deserialization is converting the serialized object back into a Java object.

Let’s first deserialize a list of strings.

Example

import java.io.*;
import java.util.*;

class Test {

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

    try {
      FileInputStream inputStream = new FileInputStream("list_of_cities");
      ObjectInputStream objectInputStream = new ObjectInputStream(inputStream );

      cities = (ArrayList) objectInputStream.readObject();

      objectInputStream.close();
      inputStream .close();
    } catch (IOException ioe) {
      ioe.printStackTrace();
      return;
    } catch (ClassNotFoundException c) {
      c.printStackTrace();
      return;
    }
    // print the cities
    for (String city : cities) {
      System.out.println(city);
    }
  }
}
Output: Paris Rome London Tokyo

Deserialize an ArrayList of custom objects

Below is the program that reads the list of a Person objects from a byte stream.

import java.io.*;
import java.util.*;

class Person implements Serializable {

  private String name;
  private int age;

 // constructors, getters, setters and toString() method
}

class Test {

  public static void main(String[] args) {
    ArrayList<Person> persons = new ArrayList<>();

    try {
      FileInputStream inputStream = new FileInputStream("list_of_persons");
      ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);

      persons = (ArrayList) objectInputStream.readObject();

      objectInputStream.close();
      inputStream.close();
    } catch (IOException ioe) {
      ioe.printStackTrace();
      return;
    } catch (ClassNotFoundException c) {
      c.printStackTrace();
      return;
    }

    //print the person objects from the list
    for (Person person : persons) {
      System.out.println(person);
    }
  }
}
Output: Person{name=’John’, age=32} Person{name=’Alek’, age=31} Person{name=’Megan’, age=27} Person{name=’Melissa’, age=25}
 
And that is how you can Serialize and Deserialize an ArrayList in Java.

Leave a Reply

Your email address will not be published.