In this article, we will understand how to use ObjectOutputStream class in Java with an example.
1. Introduction to ObjectInputStream
ObjectInputStream class is a part of the java.io package, the ObjectInputStream is responsible for deserialization. It reads primitive data types and reconstitutes objects previously serialized by an ObjectOutputStream.
2. Constructing an ObjectInputStream
Typically, you initialize an ObjectInputStream by wrapping around another InputStream instance:
// File name
String filename = "student.dat";
// Deserialize the Student object
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
3. Reading Objects
Use the readObject method to deserialize an object. Ensure that the object's class (and any objects referenced by it) implements the Serializable interface:
Student student = (Student) ois.readObject();
4. Other Useful Methods
readUnshared(): Reads an "unshared" object, ensuring that subsequent references to the object resolve to a new instance during deserialization.
5. Considerations
Versioning: Any changes (addition or removal of fields) in the class after serialization might cause issues during deserialization unless managed via the serialVersionUID.
Security: Always be cautious with deserializing objects from untrusted sources. It might pose security risks.
Transient Fields: Fields marked as transient aren't serialized. Upon deserialization, they'll have their default values.
6. Closing the Stream
Use try-with-resources to automatically close the resource: // Deserialize the Student object
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
Student student = (Student) ois.readObject();
System.out.println("Deserialized student data: " + student);
} catch (IOException | ClassNotFoundException e) {
System.out.println("An error occurred: " + e.getMessage());
}
7. Example
1. Create Student Class
Let's create a basic Student class that implements Serializable and add the following code to it:import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int rollNo;
private double gradePoint;
public Student(String name, int rollNo, double gradePoint) {
this.name = name;
this.rollNo = rollNo;
this.gradePoint = gradePoint;
}
@Override
public String toString() {
return "Student{name='" + name + "', rollNo=" + rollNo + ", gradePoint=" + gradePoint + '}';
}
}
2. Deserialization using ObjectInputStream
Now, let's deserialize a Student object from a file named "student.dat" and print its details.
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
public class DeserializeStudent {
public static void main(String[] args) {
// File name
String filename = "student.dat";
// Deserialize the Student object
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
Student student = (Student) ois.readObject();
System.out.println("Deserialized student data: " + student);
} catch (IOException | ClassNotFoundException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}
// Expected Output:
// Deserialized student data: Student{name='John', rollNo=101, gradePoint=3.9}
Note: For the above deserialization example to work, a Student object should have been serialized and saved to "student.dat" beforehand using ObjectOutputStream.
Related Java I/O Classes
- FileOutputStream Class in Java
- FileInputStream class in Java
- ByteArrayOutputStream Class in Java
- ByteArrayInputStream Class in Java
- DataOutStream Class in Java
- DataInputStream Class in Java
- ObjectOutputStream Class in Java
- ObjectInputStream Class in Java
- BufferedOutputStream Class in Java
- BufferedInputStream Class in Java
- BufferedWriter Class in Java
- BufferedReader Class in Java
- FileWriter Class in Java
- FileReader Class in Java
Comments
Post a Comment
Leave Comment