How to Write an Object to file in Java

By | April 12, 2018

In this java tutorial i will share about “How to Write an Object to file in Java”. Java object can write into a file for future access, this is called Serialization. In order to do this, you have to implement the Serializable interface, and use ObjectOutputStream to write objects into a file.

FileOutputStream fout = new FileOutputStream("c:\\temp\\address.ser");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(address);

How to Write an Object to file in Java

1. Object

Create an “Address” object and implement the Serializable interface. This object is going to write into a file.

Address.java
package com.cimblo.io;

import java.io.Serializable;

public class Address implements Serializable {

	private static final long serialVersionUID = 1L;

	String street;
	String country;

	public void setStreet(String street) {
		this.street = street;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getStreet() {
		return this.street;
	}

	public String getCountry() {
		return this.country;
	}

	@Override
	public String toString() {
		return new StringBuffer(" Street : ")
				.append(this.street).append(" Country : ")
				.append(this.country).toString();
	}

}

2. Write Object to File

This class will write the “Address” object and it’s variable value (“wall street”, “united state”) into a file named c:\\temp\\address.ser

WriteObject.java
package com.cimblo.io;

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

public class WriteObject {

	public static void main(String args[]) {

		WriteObject obj = new WriteObject();

		Address address = new Address();
		address.setStreet("wall street");
		address.setCountry("united state");

		obj.serializeAddress(address);
	}

	public void serializeAddress(Address address) {

		FileOutputStream fout = null;
		ObjectOutputStream oos = null;

		try {

			fout = new FileOutputStream("c:\\temp\\address.ser");
			oos = new ObjectOutputStream(fout);
			oos.writeObject(address);
			System.out.println("Done");

		} catch (Exception ex) {

			ex.printStackTrace();

		} finally {

			if (fout != null) {
				try {
					fout.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (oos != null) {
				try {
					oos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void serializeAddressJDK7(Address address) {
		try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("c:\\temp\\address2.ser"))) {
			oos.writeObject(address);
			System.out.println("Done");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

References

  1. ObjectOutputStream JavaDoc
  2. Java Object Serialization

One thought on “How to Write an Object to file in Java

  1. Pingback: How to Read an Object from file in Java ~ Cimblo.com

Leave a Reply