Home » Core Java » io » FileOutputStream » How to Write an Object to File in Java

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

How to Write an Object to File in Java

On this tutorial we are going to see how you can store an object to a File in your File system in Java. Basically to perform basic IO operations on object, the class of the object has to implement the Serializable interface. This gives the basic interface to work with IO mechanisms in Java.

In short, in order to write an object to a file one should follow these steps:

  • Create a class that implements the Serializable interface.
  • Open or create a new file using FileOutputStream.
  • Create an ObjectOutputStream giving the above FileOutputStream as an argument to the constructor.
  • Use ObjectOutputStream.writeObject method to write the object you want to the file.

Let’s take a look at the code snippets that follow:


package com.javacodegeeks.java.core;

import java.io.Serializable;

public class Student implements Serializable {

	//default serialVersion id
	private static final long serialVersionUID = 1L;

	private String first_name;
	private String last_name;
	private int age;

	public Student(String fname, String lname, int age){
		this.first_name = fname;
		this.last_name  = lname;
		this.age        = age;

	public void setFirstName(String fname) {
		this.first_name = fname;

	public String getFirstName() {
		return this.first_name;

	public void setLastName(String lname) {
		this.first_name = lname;

	public String getLastName() {
		return this.last_name;

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

	public int getAge() {
		return this.age;

	public String toString() {
		return new StringBuffer(" First Name: ").append(this.first_name)
				.append(" Last Name : ").append(this.last_name).append(" Age : ").append(this.age).toString();



package com.javacodegeeks.java.core;

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

public class ObjectIOExample {

	private static final String filepath="C:\\Users\\nikos7\\Desktop\\obj";

	public static void main(String args[]) {

		ObjectIOExample objectIO = new ObjectIOExample();

		Student student = new Student("John","Frost",22);

	public void WriteObjectToFile(Object serObj) {

		try {

			FileOutputStream fileOut = new FileOutputStream(filepath);
			ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
			System.out.println("The Object  was succesfully written to a file");

		} catch (Exception ex) {


The Object  was succesfully written to a file

This was an example on how to write an Object to a File in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....



Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.