Java ObjectInputStream Class
What is Java ObjectInputStream Class?
Java ObjectInputStream class is a class that is used to deserialize Java objects from an input stream.It is commonly used in client-server applications and in file handling.
It is a part of the Java IO package and is used to read Java objects that have been previously serialized. ObjectInputStream reads the serialized objects and reconstructs them into their original state.
To understand the Java ObjectInputStream, Read the Complete Article.
Working of ObjectInputStream
- Here’s how ObjectInputStream works:
ObjectInputStream reads a stream of bytes from an input source, such as a file or a network socket.
The stream of bytes must contain data that was previously serialized using ObjectOutputStream.
ObjectInputStream uses a combination of reflection and metadata embedded in the serialized data to reconstruct the original object.
As it reads the serialized data, ObjectInputStream checks the class hierarchy to ensure that the serialized data matches the expected type.
ObjectInputStream also performs some security checks to prevent malicious code from being executed during deserialization.
Once the deserialization process is complete, the reconstructed object can be used just like any other Java object.
It is important to note that the deserialization process can be resource-intensive, especially for large or complex objects. Therefore, it is important to use ObjectInputStream judiciously and to avoid deserializing untrusted data.
Create an ObjectInputStream
How to create an ObjectInputStream in Java:
- First, open an input stream from a data source, such as a file or a network socket.
- Next, create a new ObjectInputStream object, passing the input stream to its constructor.
- Wrap the ObjectInputStream constructor in a try-catch block to handle any potential exceptions, such as IOException or ClassNotFoundException.
- Use the readObject() method of the ObjectInputStream to read serialized objects from the input stream.
- Once you have finished reading objects from the input stream, close the ObjectInputStream to release any resources it may be holding.
Method Of ObjectInputStream:
Methods | Description |
---|---|
readObject(): | Reads and deserializes the next object from the input stream. |
read(): | Reads a byte of data from the input stream. |
readByte(): | Reads a byte value from the input stream. |
readShort(): | Reads a short value from the input stream. |
close(): | Closes the ObjectInputStream and releases any system resources associated with it. |
remove(element): | Removes the first occurrence of the specified element from the Vector. |
readFloat(): | Reads a float value from the input stream. |
available(): | Returns an estimate of the number of bytes that can be read from the input stream. |
There are many more methods available, but these are the most commonly used ones.
Example 1: Java ObjectInputStream
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; class Main { public static void main (String[]args) { int data1 = 72; String data2 = "PrepInsta is a e-learning Platform"; try { FileOutputStream file = new FileOutputStream ("file.txt"); ObjectOutputStream output = new ObjectOutputStream (file); // Writing to the file using ObjectOutputStream output.writeInt (data1); output.writeObject (data2); FileInputStream fileStream = new FileInputStream ("file.txt"); // Creating an object input stream ObjectInputStream objStream = new ObjectInputStream (fileStream); //Using the readInt() method System.out.println ("Integer data :" + objStream.readInt ()); // Using the readObject() method System.out.println ("String data: " + objStream.readObject ()); output.close (); objStream.close (); } catch (Exception e) { e.getStackTrace (); } } }
Output
Integer data :72 String data: PrepInsta is a e-learning Platform
Example 2 : Java ObjectInputStream Class
import java.io.*; public class Main { public static void main(String[] args) { // Create an object to be written to a file Person person = new Person("John", "Doe", 30); try { // Write the object to a file using ObjectOutputStream FileOutputStream fileOutputStream = new FileOutputStream("person.dat"); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(person); // Read the object from the file using ObjectInputStream FileInputStream fileInputStream = new FileInputStream("person.dat"); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); Person readPerson = (Person) objectInputStream.readObject(); // Print the read object System.out.println("Name: " + readPerson.getFirstName() + " " + readPerson.getLastName()); System.out.println("Age: " + readPerson.getAge()); // Close the streams objectOutputStream.close(); objectInputStream.close(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } } class Person implements Serializable { private String firstName; private String lastName; private int age; public Person(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public int getAge() { return age; } }
Output
Name: John Doe Age: 30
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment