Introduction
The ObjectInputStream
class in Java is part of the java.io
package and is used to deserialize objects previously written using an ObjectOutputStream
. This class is essential for reading objects and primitive data types from a stream that has been serialized using Java's built-in serialization mechanism. It enables the reconstruction of objects from a stream, maintaining their state as it was during serialization.
Table of Contents
- Creating an ObjectInputStream
- Reading Objects with ObjectInputStream
- Closing the Stream
- Complete Example
- Conclusion
Creating an ObjectInputStream
To create an ObjectInputStream
, you need to wrap it around another input stream, such as a FileInputStream
. This allows you to read serialized objects from a file or another input source.
Example
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ObjectInputStreamExample {
public static void main(String[] args) {
try {
// Creating ObjectInputStream using FileInputStream
FileInputStream fis = new FileInputStream("example.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the example above, ObjectInputStream
is created using a FileInputStream
to read from a file named example.ser
.
Reading Objects with ObjectInputStream
The ObjectInputStream
class provides methods to read objects and primitive data types from the input stream:
Object readObject()
: Reads an object from the input stream.boolean readBoolean()
: Reads a boolean value.byte readByte()
: Reads an 8-bit byte.char readChar()
: Reads a 16-bit character.double readDouble()
: Reads a 64-bit double.float readFloat()
: Reads a 32-bit float.int readInt()
: Reads a 32-bit integer.long readLong()
: Reads a 64-bit long.short readShort()
: Reads a 16-bit short.
Example
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class ObjectInputStreamReadExample {
public static void main(String[] args) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("example.ser"))) {
int intValue = ois.readInt();
double doubleValue = ois.readDouble();
boolean booleanValue = ois.readBoolean();
String stringValue = (String) ois.readObject();
System.out.println("Integer Value: " + intValue);
System.out.println("Double Value: " + doubleValue);
System.out.println("Boolean Value: " + booleanValue);
System.out.println("String Value: " + stringValue);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
In the example above, ObjectInputStream
reads an integer, a double, a boolean, and a string from a file named example.ser
.
Closing the Stream
It is important to close the ObjectInputStream
after completing the I/O operations to release the system resources associated with the stream. This can be done using the close()
method.
Example
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class ObjectInputStreamCloseExample {
public static void main(String[] args) {
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("example.ser"));
int intValue = ois.readInt();
double doubleValue = ois.readDouble();
boolean booleanValue = ois.readBoolean();
String stringValue = (String) ois.readObject();
System.out.println("Integer Value: " + intValue);
System.out.println("Double Value: " + doubleValue);
System.out.println("Boolean Value: " + booleanValue);
System.out.println("String Value: " + stringValue);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (ois != null) {
ois.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
In the example above, the ObjectInputStream
is closed in the finally
block to ensure that it is closed even if an exception occurs.
Alternatively, you can use the try-with-resources statement, which ensures that the stream is closed automatically.
Example
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
public class ObjectInputStreamTryWithResourcesExample {
public static void main(String[] args) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("example.ser"))) {
int intValue = ois.readInt();
double doubleValue = ois.readDouble();
boolean booleanValue = ois.readBoolean();
String stringValue = (String) ois.readObject();
System.out.println("Integer Value: " + intValue);
System.out.println("Double Value: " + doubleValue);
System.out.println("Boolean Value: " + booleanValue);
System.out.println("String Value: " + stringValue);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
In the example above, the try-with-resources statement is used to automatically close the ObjectInputStream
.
Complete Example
Here is a complete example demonstrating the creation, reading, and closing of an ObjectInputStream
.
ObjectInputStreamExample.java
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
public class ObjectInputStreamExample {
public static void main(String[] args) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("example.ser"))) {
int intValue = ois.readInt();
double doubleValue = ois.readDouble();
boolean booleanValue = ois.readBoolean();
String stringValue = (String) ois.readObject();
System.out.println("Integer Value: " + intValue);
System.out.println("Double Value: " + doubleValue);
System.out.println("Boolean Value: " + booleanValue);
System.out.println("String Value: " + stringValue);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
In this example, an ObjectInputStream
is created, data is read from a file, and the stream is automatically closed using the try-with-resources statement.
Conclusion
The ObjectInputStream
class in Java is used for reading serialized objects and primitive data types from an input stream. By understanding how to create, read, and close an ObjectInputStream
, you can effectively handle deserialization in your Java applications. Remember to always close the stream after use to ensure that system resources are properly released.
Comments
Post a Comment
Leave Comment