Responsive Ad Area
Home » Blog » Reading and Writing Serialized Objects to a File

Share This Post

Featured Tutorials / Java

Reading and Writing Serialized Objects to a File

Reading and Writing Serialized Objects to a File

Pev post in this trail is Java Fixed Width Text files using row, col type storage.

In this article we will write some objects to a file then read them back. The objects need to be Serializable. Later in an article I will show you how to serialize Java to xml and back but standard serialization uses some code that is unreadable. So to make the object file we use.

1. Create FiieOutputStream giving it a file name or File Object.
2. Create an ObjectStream giving it the FileOutputStream
3. use objectStream.writeObject(someObject);

To read the object file we use

1. Create FileInputStream and we can give it a file name or File object
2. Create ObjectInputStream using ObjectInputStream
3. use (SomeObject)oubjectInputStream.readObject(someObject) casting it to that object.

If the file contains only one object then we should be fine. If it contains multiple kinds of objects I would assume we need to cast them to the correct objects as they are read in. Meaning we would need to know the order that they are written and read back in. Another thing to note, I had to use a tool called serialver.exe to make the version number for the Expense object. You get this by running serialver and giving it the Expense.class file name. Also if its an inner class you must make it static or it won't work. Its generally suggested to never use an inner class for this purpose as it may lead to problems, but I did for this example.

The code below works but it needs file close statements and it throws and excpetion when it reaches end of file. I'll fix that soon I hope.

import java.util.*;

public class ObjectWriterReader{
 public static class Expense implements Serializable{
  private static final long serialVersionUID = -3601370280790552486L;
  public Expense(float amount,Date date, String catagory, String note){
  /** @serial */
  float amount=0.0f;
  /** @serial */
  Date date=null;
  /** @serial */
  String catagory="";
  /** @serial */
  String note="";
  public String toString(){
     return "Amount:"+amount+" Date:"+date+" Catagory:"+catagory+" Note:"+note+"\n";
 ArrayList<Expense> expenses =  new ArrayList<Expense>();
 public ObjectWriterReader(){
  Expense anExpense=new Expense(22.25f,new Date("10/2/18"),"Clothing","Jeans");
  anExpense=new Expense(55.14f,new Date("10/3/18"),"Fuel","Gas  2.34/gal");
  anExpense=new Expense(1.08f,new Date("10/5/18"),"Food","Coffee");
  anExpense=new Expense(34.25f,new Date("10/9/18"),"Bar","Food and Drinks");
  Iterator<Expense> expenseIterator = expenses.iterator();
   FileOutputStream fos = new FileOutputStream("expenses.dat", true);
   ObjectOutputStream oos = new ObjectOutputStream(fos);
  }catch(Exception e){
  try {
   expenses=new ArrayList<Expense>();
   FileInputStream fis = new FileInputStream("expenses.dat");
   ObjectInputStream ois = new ObjectInputStream(fis);
   Expense expenseObject=null;
    expenseObject = (Expense)ois.readObject();
    if(expenseObject!=null) expenses.add(expenseObject);
  } catch (Exception e) {
  Expense expense=null;
  expenseIterator = expenses.iterator();
 public static void main(String args[]){
  new ObjectWriterReader();

Pev post in this trail is Java Fixed Width Text files using row, col type storage.


Share This Post

1 Comment

  1. Ahhh the Zen of Arksoft…I can feel the data flowing through me. 🙂

Leave a Reply

Skip to toolbar