ObjectInputStream and ObjectOutputStream in Java
Introduction to Object Serialization and De-serialization
"
Serialization and De-serialization of an Array of Objects
"
Introduction to ObjectInputStream and ObjectOutputStream Classes in Java
ObjectInputStream and ObjectOutputStream are two classes in Java that are used to serialize and deserialize Java objects, respectively. Serialization is the process of converting a Java object into a stream of bytes that can be stored or transmitted. Deserialization is the process of converting a stream of bytes back into a Java object.
ObjectInputStream and ObjectOutputStream are used in a variety of applications, such as:- Persisting Java objects to a file or database
- Sending Java objects over a network
- Saving and restoring the state of a Java object
ObjectInputStream and ObjectOutputStream are very powerful tools for serializing and deserializing Java objects. They are used in a wide variety of applications, and they can make it very easy to store and transmit Java objects.
Here are some additional things to keep in mind when using ObjectInputStream and ObjectOutputStream:- ObjectInputStream and ObjectOutputStream are both AutoCloseable classes. This means that they can be automatically closed using the try-with-resources statement.
- If you are writing to a file that does not exist, ObjectOutputStream will create the file for you.
- If you are writing to a file that already exists, ObjectOutputStream will overwrite the contents of the file.
- To append data to an existing file, you can use the ObjectOutputStream constructor that takes a boolean parameter. If you set this parameter to true, ObjectOutputStream will append data to the end of the file instead of overwriting it.
Methods of ObjectInputStream Class
Method | Description |
---|---|
available() | Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected. |
close() | Closes this input stream and releases any system resources associated with this stream. |
mark(int readlimit) | Marks the current position in this input stream. A subsequent reset() will attempt to reposition the stream to this point. |
markSupported() | Tells whether this input stream supports the mark() and reset() methods. |
read() | Reads a byte of data from this input stream. |
read(byte[] b) | Reads up to b.length bytes of data from this input stream into an array of bytes. |
read(byte[] b, int off, int len) | Reads up to len bytes of data from this input stream into an array of bytes, starting at offset off in the array. |
readObject() | Reads an object from the input stream. |
reset() | Repositions this stream to the position at which the last mark() was set. |
skip(long n) | Skips over and discards n bytes of data from this input stream. |
Methods of ObjectOutputStream Class
Method | Description |
---|---|
close() | Closes this output stream and releases any system resources associated with this stream. |
flush() | Flushes this output stream and forces any buffered output bytes to be written out to the underlying device. |
writeObject(Object obj) | Writes an object to the output stream. |
writeUnshared(Object obj) | Writes an object to the output stream without sharing. |
Exercises
Serialize and Deserialize an Employee object using a File
package javaprogrammingdemo;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Scanner;
public class javalabclass{
public static void main(String args[])
{
try
{
employee e;
System.out.println("Enter the Employee Details");
Scanner input = new Scanner(System.in);
String name,empid,address,phone;
ObjectOutputStream objout = new ObjectOutputStream(new FileOutputStream("satish.txt"));
System.out.println("Enter the employee name");
name=input.nextLine();
System.out.println("Enter the employee empid");
empid=input.nextLine();
System.out.println("Enter the employee address");
address=input.nextLine();
System.out.println("Enter the employee phone");
phone = input.nextLine();
e=new employee(name,empid,address,phone);
//Writing the object to a file
objout.writeObject(e);
objout.close();
//reading the object from the input file
ObjectInputStream objin = new ObjectInputStream(new FileInputStream("satish.txt"));
Object obj = null;
obj = objin.readObject();
System.out.println("The name of the employee is ");
System.out.println(((employee)obj).name);
objin.close();
}
catch(Exception e)
{
System.out.println("I am catching exception here");
System.out.println(e.getMessage());
}
}
}
class employee implements Serializable
{
String name;
String empid;
String address;
String phone;
public employee(String name, String empid, String address, String phone) {
this.name = name;
this.empid = empid;
this.address = address;
this.phone = phone;
}
}
Read Input for an Array of Employee Objects from the user. Write the Array of Objects to a file. Read the objects from the file and display the Employee Names. Use ObjectInputStream and ObjectOutputStream for file operations.
package javalabdemo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Scanner;
public class javalabclass {
public static void main(String[] args) throws InterruptedException, FileNotFoundException, IOException, ClassNotFoundException {
try
{
employee e[] = new employee[2];
System.out.println("Enter the Employee Details");
Scanner input = new Scanner(System.in);
String name,empid,address,phone;
ObjectOutputStream objout = new ObjectOutputStream(new FileOutputStream("satish.txt"));
for(int i=0;i<e.length;i++)
{
System.out.println("Enter the employee name");
name=input.nextLine();
System.out.println("Enter the employee empid");
empid=input.nextLine();
System.out.println("Enter the employee address");
address=input.nextLine();
System.out.println("Enter the employee phone");
phone = input.nextLine();
e[i]=new employee(name,empid,address,phone);
objout.writeObject(e[i]);
}
objout.writeObject(new endoffile());
objout.close();
//reading the objects from the input file
ObjectInputStream objin = new ObjectInputStream(new FileInputStream("satish.txt"));
Object obj = null;
while((obj = objin.readObject()) instanceof endoffile == false){
System.out.println(((employee)obj).name);}
objin.close();
}
catch(Exception e)
{
System.out.println("I am catching exception here");
System.out.println(e.getMessage());
}
}
}
class employee implements Serializable
{
String name;
String empid;
String address;
String phone;
public employee(String name, String empid, String address, String phone) {
this.name = name;
this.empid = empid;
this.address = address;
this.phone = phone;
}
}
class endoffile implements Serializable
{
}