How to Serialize an array or Collection of objects

Hope you have already gone thru basic working of Serialization. To Serialize an array of objects of a class, below are the steps.
1. The class whose array of objects need to be serialized, has to implement java.io.Serializable interface
2. Create another class, which has an array or Collection of objects, as a data member. This class also has to implement Serializable interface.
3. Now, you can follow normal steps to Serialize class created in above, step2

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication2;

import java.io.Serializable;

class StudentInfo implements Serializable
{
 //transient data members will not take part in Serialization   
 String name;
int rid;
 StudentInfo(String n, int r)
 {
  this.name = n;
  this.rid = r;
 }

 void printStudentInfo()
 {
     System.out.println("StudentInfo details Test:"+name+" "+rid+" ");
 }
 
 public String toString()
 {
     return "name="+name+", id="+rid;
 }
}


class StudentInfoArray implements Serializable{
    StudentInfo arr[];
    
    StudentInfoArray(StudentInfo arr[])
    {
        this.arr = arr;
    }
}
package javaapplication2;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializeArray
{
    
 public static void main(String[] args)
 {
 try
 {
//Serialization starts here
  StudentInfo si1 = new StudentInfo("checkingnexttime", 310);
  
  StudentInfo si2 = new StudentInfo("checkingnexttime1", 311);
  
  StudentInfo si3 = new StudentInfo("checkingnexttime2", 312);
  
  StudentInfo sarr[] = {si1, si2, si3};
  
  StudentInfoArray sia = new StudentInfoArray(sarr);
  
  //.ser is a standard file extension, to store serialized object
  FileOutputStream fos = new FileOutputStream("C:\\student.ser");

  //instead of writing object stream to a file, it is also possible to send object thru 
  //Network using socket programming
  ObjectOutputStream oos = new ObjectOutputStream(fos);

  System.out.println("Serialing the object...");
  oos.writeObject(sia);
  oos.close();
  fos.close();  
  }
  catch (Exception e)
  { e. printStackTrace(); }
 }
}
package javaapplication2;

import java.io.*;

public class DeSerializeArray {
 public static void main(String[] args)
 {
 try
 {
  
  System.out.println("De serializing object...");
  //DeSerialization starts here
  FileInputStream fis = new FileInputStream("C:\\student.ser");
  ObjectInputStream ois = new ObjectInputStream(fis);
  StudentInfoArray toa = (StudentInfoArray)ois.readObject();

  System.out.println("before printing Object");

  for(StudentInfo si:toa.arr)//foreach
  {
      System.out.println(si);
  }
    
  //RMI(Remote Method Invocation) is based on Serialization/Deserialization
  }
  catch (Exception e)
  { e. printStackTrace(); }
    }
}