import java.io.*;
public class Student implements Serializable {
private int rollNo;
private String name;
transient int age;
public Student(int rollNo, String name, int age) {
this.rollNo = rollNo;
this.name = name;
this.age = age;
}
public int getRollNo() {
return rollNo;
}
public String getName() {
return name;
}
}
-----------------------------------------------------------
import java.io.*;
public class TestObjectInputStream {
public static void main(String s[]) throws IOException,
ClassNotFoundException {
/*FileInputStream fileInStream =
new FileInputStream("Student.ser");
ObjectInputStream objInStream =
new ObjectInputStream(fileInStream);*/
ObjectInputStream objInStream =
new ObjectInputStream(
new FileInputStream("Student.ser"));
System.out.println("Deserializing object s1...");
Student s1 = (Student) objInStream.readObject();
System.out.println("Object s1 deserialized.");
System.out.println("Roll No.: " + s1.getRollNo());
System.out.println("Name : " + s1.getName());
System.out.println("Age : " + s1.age);
objInStream.close();
}
}
--------------------------------------------------------------------
import java.io.*;
public class TestObjectOutputStream {
public static void main(String s[]) throws IOException {
/*FileOutputStream fileOutStream =
new FileOutputStream("Student.ser");
ObjectOutputStream objOutStream =
new ObjectOutputStream(fileOutStream);*/
ObjectOutputStream objOutStream =
new ObjectOutputStream(
new FileOutputStream("Student.ser"));
Student s1 = new Student(12, "Arvind", 14);
System.out.println("Serializing object s1...");
objOutStream.writeObject(s1);
System.out.println("Object s1 serialized.");
objOutStream.close();
}
}
0 Comments