Serialization-with Merge two files without no redundant object

package iopack;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

import pkg.Student;

public class SerializationTest {

public static void main(String[] args)
{
Student s = new Student("Ram",5005,"MCA",5);
Student s1 = new Student("Sham",5004,"MCA",4);
Student s2 = new Student("Nanda",5003,"MCA",3);
Student s3 = new Student("Krishna",5002,"MCA",5);
Student s4 = new Student("Krishna",5002,"MCA",5);

Set <Student> stt = new TreeSet<Student>(); // for Merging

ObjectOutputStream oos = null;

try
{
oos = new ObjectOutputStream(new FileOutputStream("D:\\Stud.ser"));
oos.writeObject(s);
oos.writeObject(s1);
oos.writeObject(s2);
oos.writeObject(s3);
oos.writeObject(s4);

System.out.println("Write done");

}

catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

catch (IOException e)
{

e.printStackTrace();
}
finally
{
try
{
oos.close();
}

catch (IOException e)
{

e.printStackTrace();
}
}

ObjectInputStream ois =null;
ObjectInputStream ois1 =null;

try
{
ois = new ObjectInputStream(new FileInputStream("D:\\Stud.ser"));
Student ss =(Student) ois.readObject();
Student ss1 =(Student) ois.readObject();
Student ss2 =(Student) ois.readObject();
Student ss3 =(Student) ois.readObject();
Student ss4 =(Student) ois.readObject();

//storing to TreeSet for Merging to a new file ..............

stt.add(ss);
stt.add(ss1);
stt.add(ss2);
stt.add(ss3);
stt.add(ss4);

System.out.println("Objectas added !");

//System.out.println(ss1+ " " +ss2 + " " + ss3);
}




catch (FileNotFoundException e)
{

e.printStackTrace();
}

catch (IOException e)
{

e.printStackTrace();
}

catch (ClassNotFoundException e)
{

e.printStackTrace();
}

finally
{
try
{
ois.close();
}

catch (IOException e)
{

e.printStackTrace();
}





//------------File Merging -----------------------------------------------------




ObjectOutputStream oosMerge = null;

try
{

oosMerge = new  ObjectOutputStream(new FileOutputStream("D:\\StudMerge.ser"));
Iterator<Student> itr = stt.iterator();
     

while(itr.hasNext())
       {
        Student ss = itr.next();
        oosMerge.writeObject(ss);
         
       }
     
       System.out.println(oosMerge);
     
       //Retrive from oosMerge file and print the data
     

   ois1 = new ObjectInputStream(new FileInputStream("D:\\StudMerge.ser"));
 
Student ss =(Student) ois1.readObject();
Student ss1 =(Student) ois1.readObject();
Student ss2 =(Student) ois1.readObject();
Student ss3 =(Student) ois1.readObject();
Student ss4 =(Student) ois1.readObject();

System.out.println(ss + "  " + ss1 + "  " + ss2 + "  " + ss3 + " " + ss4);


}

catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

catch (ClassNotFoundException e)

{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}


}
}

}