Deserialize Object Example
This program demonstrates deserializing a Java object from a file.
Problem Statement
Write a Java program that deserializes a Student object from a file using ObjectInputStream.
Source Code
| 1 | import java.io.*; |
| 2 | class Student implements Serializable { |
| 3 | int id; String name; |
| 4 | Student(int id,String name){ this.id=id; this.name=name; } |
| 5 | } |
| 6 | public class DeserializeExample { |
| 7 | public static void main(String[] args) { |
| 8 | System.out.println("Eduinq Deserialize Example"); |
| 9 | try(ObjectInputStream ois=new ObjectInputStream(new FileInputStream("student.ser"))) { |
| 10 | Student s=(Student)ois.readObject(); |
| 11 | System.out.println("Eduinq Student: "+s.id+" "+s.name); |
| 12 | } catch(Exception e){ System.out.println("Eduinq Exception: "+e.getMessage()); } |
| 13 | } |
| 14 | } |
Program Output
Eduinq Deserialize Example Eduinq Student: 1 Arun
Explanation
Uses ObjectInputStream to deserialize object. - Restores object state from file. - Exception handling ensures safe operations. - In enterprise systems, deserialization retrieves persisted objects. Conclusion: Shows deserializing objects.