Saturday, January 9, 2016

How to save/read an ArrayList to/from file in Java

Java provides a mechanism where an object can be represented as a sequence of bits that contains the data of this object and this information: type, and the types of data stored in the object.

After the serialized object has been well recorded in the file, it can be read without problems from the file and then deserialize. The bits that represent the object and its data can be used to recreate the object in memory.

The ObjectInputStream and ObjectOutputStream classes are two high-level data stream that contain the methods for recording and reading the contents of an ArrayList from a file.

ArrayList is serializable by default. This means that you do not need to implement Serializable in order to serialize an ArrayList.

Write ArrayList to file in Java

This class creates a test file that will have an ArrayList object as a stream of bits. The test file is used to save and recreate the object from bitstream. Note that we do not implement Serializable in this example because ArrayList is already serialized by default.

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class SerializeArrayList {

  public static void main(String[] args) {
     ArrayList arraylist=new ArrayList();
     arraylist.add("hello world");
     
     try {
       FileOutputStream fileOut = new FileOutputStream("file");
       ObjectOutputStream out = new ObjectOutputStream(fileOut);
       out.writeObject(arraylist);
       out.close();
       fileOut.close();
       System.out.println("\nSerialization completed successfully...\n");
 
     } catch (FileNotFoundException e) {
       e.printStackTrace();
     } catch (IOException e) {
       e.printStackTrace();
     }
  }
}
After compilation and execution:

Serialization completed successfully

Read and create an ArrayList from file

In this class, one recovers the data stream in the form of bits from the test file that was stored using the class above. We converted the returned object in ArrayList with the cast and shows the ArrayList elements. By observing the output, we obtain the same elements which are added to the list before serialization.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

public class main{

  public static void main(String[] args) {

    ArrayList<String> arraylist= new ArrayList<String>();
    try {
      FileInputStream fileis = new FileInputStream("file");
      ObjectInputStream ois = new ObjectInputStream(fileis);
      arraylist = (ArrayList) ois.readObject();
      ois.close();
      fileIn.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
       e.printStackTrace();
    }

    System.out.println("Reading data: \n");
    for(String o:arraylist)
      System.out.println(o);
  }
}

After compilation and execution:

hello world

No comments:

Post a Comment