ObjectOutputStream in Java – write Object to File

Filed Under: Java

ObjectOutputStream in Java can be used to convert an object to OutputStream. The process of converting object to stream is called serialization in java.

Once an object is converted to Output Stream, it can be saved to file or database, send over the network or used in socket connections. So we can use FileOutputStream to write Object to file.


ObjectOutputStream is part of Java IO classes and its whole purpose is to provide us a way to convert java object to a stream. When we create an instance of ObjectOutputStream, we have to provide the OutputStream to be used. This OutputStream is further used by ObjectOutputStream to channel the object stream to underlying output stream, for example, FileOutputStream.

ObjectOutputStream requirement

The object that we want to serialize should implement java.io.Serializable interface. Serializable is just a marker interface and doesn’t have any abstract method that we have to implement. We will get java.io.NotSerializableException if the class doesn’t implement Serializable interface. Something like below exception stack trace.

java.io.NotSerializableException: com.journaldev.files.EmployeeObject
	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
	at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
	at com.journaldev.files.ObjectOutputStreamExample.main(ObjectOutputStreamExample.java:21)

Java ObjectOutputStream Example to write object to file

Le,t’s look at java ObjectOutputStream example to write an object to file. For that first of all, we should have a class with some properties.

Let’s create an Object that we will save into the file.

package com.journaldev.files;

import java.io.Serializable;

public class Employee implements Serializable {

	private static final long serialVersionUID = -299482035708790407L;

	private String name;
	private String gender;
	private int age;

	private String role;
	// private transient String role;

	public Employee(String n) {
		this.name = n;

	public String getGender() {
		return gender;

	public void setGender(String gender) {
		this.gender = gender;

	public int getAge() {
		return age;

	public void setAge(int age) {
		this.age = age;

	public String getRole() {
		return role;

	public void setRole(String role) {
		this.role = role;

	public String toString() {
		return "Employee:: Name=" + this.name + " Age=" + this.age + " Gender=" + this.gender + " Role=" + this.role;


Notice that it’s not a requirement to have getter/setter for all the properties. Or to have a no-argument constructor.

As you can see that above Employee object doesn’t have getter/setter methods for “name” property. It also doesn’t have a no-argument constructor.

Here is the program showing how to write Object to file in java using ObjectOutputStream.

package com.journaldev.files;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class ObjectOutputStreamExample {

	public static void main(String[] args) {
		Employee emp = new Employee("Pankaj");

		try {
			FileOutputStream fos = new FileOutputStream("EmployeeObject.ser");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			// write object to file
			// closing resources
		} catch (IOException e) {


Below image shows the output of the above program.

java ObjectOutputStream example to write object to file

If you are wondering what is the content of EmployeeObject.ser file, it’s a bit garbled and something like below.


ObjectOutputStream with a transient

If we don’t want some object property to be converted to stream, we have to use the transient keyword for that. For example, just change the role property like below and it will not be saved.

private transient String role;

ObjectOutputStream and serialVersionUID

Did you noticed the serialVersionUID defined in the Employee object? It’s used by ObjectOutputStream and ObjectInputStream classes for write and read object operations. Although it’s not mandatory to have this field, but you should keep it. Otherwise anytime you change your class that don’t have effect on earlier serialized objects, it will start failing. For a detailed analysis, go over to Serialization in Java.

If you are wondering whether our program worked fine or not, use below code to read an object from the saved file.

FileInputStream is = new FileInputStream("EmployeeObject.ser");
ObjectInputStream ois = new ObjectInputStream(is);
Employee emp = (Employee) ois.readObject();

//Output will be "Employee:: Name=Pankaj Age=35 Gender=Male Role=CEO"

That’s all about java ObjectOutputStream and how to use it to write the object to file.

You can checkout more Java IO examples from our GitHub Repository.

Reference: API Doc


  1. Anirban says:

    This is my problem. Why the file should content this?


    It should contain, as per my requirement ->


    Can you please help?

  2. Dipti says:

    any real time example…?

  3. violet says:

    Why do we have to implement the serialisable interface while working with hibernate?

    1. Natraj says:

      Because the session factory expects a class to be serialized during it invocation to server with the help of session. I am not sure if its reqd for every entity object or just for class that is represented as composite key. Pankaj please provide your comment here

  4. Naveen J says:

    what is the use of
    private static final long serialVersionUID = -299482035708790407L;

    1. shikari says:

      it acts as a unique id that helps when the deserialisation happens on other machine when you try to run the same program,

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors