Object Reading writing examples



import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class ObjectReadingFromFile {

    public static void main(String[] args) {
        try {
           
        FileInputStream fin = new FileInputStream("c:/test/student.dat");
       
        ObjectInputStream objin = new ObjectInputStream(fin);
     
       
        StudentData sobj1 = (StudentData)objin.readObject();
         StudentData sobj2 = (StudentData)objin.readObject();
       
         //java.util.Date myd = (java.util.Date)objin.readObject();
       
            System.out.println("my object creation date and time :");
           // System.out.println(myd);
            System.out.println("student infor..");
            System.out.println(sobj1.getId() + " and name : " + sobj1.getName());
            System.out.println("Marks of student1 :");
            //StudentMarks sm = sobj1.getSm();
           
            //System.out.println(sm.getSub1() + " " + sm.getSub2() + " " + sm.getSub3());
           
            System.out.println("student2 data ");
            System.out.println(sobj2.getId() + " and name : " + sobj2.getName());
            System.out.println("Marks of student1 :");
            // StudentMarks sm2 = sobj2.getSm();
            //System.out.println(sm2.getSub1() + " " + sm2.getSub2() + " " + sm2.getSub3());
       
       
        } catch (Exception e) {
           
            e.printStackTrace();
        }
       
       
    }
}

// Second application




import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

public class TestObjectWritingReading {

    public static void main(String[] args) {
        // TODO code application logic here0
        StudentMarks sm1 = new StudentMarks(40,50,60);
        StudentMarks sm2 = new StudentMarks(70,80,90);
        StudentData sd1 = new StudentData(1,"Udit", sm1);
        StudentData sd2 = new StudentData(2,"Ganesh",sm2);
        try {
           
        FileOutputStream fout = new FileOutputStream("c:/test/student.dat");
        ObjectOutputStream objout = new ObjectOutputStream(fout);
       
        objout.writeObject(sd1);
        objout.writeObject(sd2);
        objout.writeObject(new Date());
       
        }
        catch (Exception e) {
            e.printStackTrace();
           
        }
       
       
    }
}



Comments

Popular posts from this blog

Custom Exception example for BankAccount

HashMap HashSet example

Annotation