Java - ObjectInputStream and ObjectOutputStream example

Posted on November 6, 2016


An ObjectOutputStram class is used to store the primitive data type and graph of Java object to an output stream, whereas an ObjectInputStream class is used to read the primitive data type and graph of Java object from an input stream.

Note - Only those objects whose class implements java.io. Serializable interface are written to stream.

Here is an example to demonstrate the ObjectOutputStram and ObjectInputStream.

User.java

package com.boraji.tutorial.io;

import java.io.Serializable;

/**
 * @author imssbora 
 */
public class User implements Serializable {

	private static final long serialVersionUID = -3389363800876569458L;
	private int id;
	private String name;

	public User(int id, String name) {
		this.id = id;
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public String toString() {
		return "ID="+this.id+" NAME="+this.name;
	}
}

In the above example, User class implement the Serializable interface, now we can store its object to an output stream.

ObjectInputOutputStreamExample.java

package com.boraji.tutorial.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

/**
 * @author imssbora
 */
public class ObjectInputOutputStreamExample {
	public static void main(String[] args) {
		File file = new File("file.txt");

		/* Write object and primitive data type to output stream */
		FileOutputStream fileOutputStream = null;
		ObjectOutputStream outputStream = null;
		try {
			fileOutputStream = new FileOutputStream(file);
			outputStream = new ObjectOutputStream(fileOutputStream);
			// Write int value
			outputStream.writeInt(500);
			// Write date
			outputStream.writeObject(new Date());
			// Write User object
			outputStream.writeObject(new User(1, "imssbora"));
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fileOutputStream != null) {
					fileOutputStream.close();
				}
				if (outputStream != null) {
					outputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		/* Read object and primitive data type from input stream */
		FileInputStream fileInputStream = null;
		ObjectInputStream objectInputStream = null;
		try {
			fileInputStream = new FileInputStream(file);
			objectInputStream = new ObjectInputStream(fileInputStream);
			// Read int
			int value = objectInputStream.readInt();
			System.out.println(value);
			// Read date
			Date date = (Date) objectInputStream.readObject();
			System.out.println(date);
			// Read User object
			User user = (User) objectInputStream.readObject();
			System.out.println(user);

		} catch (IOException | ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fileInputStream != null) {
					fileInputStream.close();
				}
				if (objectInputStream != null) {
					objectInputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

Output

500
Sun Nov 06 10:21:52 IST 2016
ID=1 NAME=imssbora