Menu
Topics Index
...
`


Input/Output: Exploring java.io > Serialization >
Siva Nookala - 14 Apr 2016
An ObjectOutputStream writes primitive data types and Java objects to an OutputStream(could be a file or a socket). The objects can be read (reconstituted) using an ObjectInputStream.

We can store the Objects in the file or a network socket so that we can use them where ever they are needed. Only objects that support the java.io.Serializable interface can be written to streams.
The method writeObject is used to write an object to the stream. Any object, including Strings and arrays, is written with writeObject. Multiple objects or primitives can be written to the stream. The objects must be read back from the corresponding ObjectInputStream with the same types and in the same order as they were written. For example to write an object that can be read by the example in ObjectInputStream:
FileOutputStream fos = new FileOutputStream("t.tmp");
      ObjectOutputStream oos = new ObjectOutputStream(fos);

      oos.writeInt(12345);
      oos.writeObject("Today");
      oos.writeObject(new Date());

      oos.close();
Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:
private void readObject(java.io.ObjectInputStream stream)
     throws IOException, ClassNotFoundException;

private void writeObject(java.io.ObjectOutputStream stream)
     throws IOException

private void readObjectNoData()
     throws ObjectStreamException;

The writeObject method is responsible for writing the state of the object for its particular class so that the corresponding readObjectmethod can restore it.
import java.io.Serializable;

public class OriginalClass implements Serializable
{
    public String someName;
    
    OriginalClass(String name)
    {
        this.someName = name;
    }
    public String toString()
    {
        return someName;
    }
}
import java.io.*;
public class WritingData
{
    public static void main(String[] args)
    {
        new WritingData().writeObjects("somefile.txt");
        
    }
    public void writeObjects(String file)
    {
        ObjectOutputStream oos = null;
        try
        {
            oos = new ObjectOutputStream(new FileOutputStream(file)); //creates a file in current directory
            
            OriginalClass [] oc = new OriginalClass[4];
            oc[0] = new OriginalClass("Prem");
            oc[1] = new OriginalClass("Siva"); // writing objects to file
            oc[2] = new OriginalClass("Srinath");
            oc[3] = new OriginalClass("Hemanth");
            
            for(int i = 0; i < oc.length; i++)
            {
                oos.writeObject(oc[i]);
            }
            System.out.println("Success");
            
        }
        catch(FileNotFoundException ex)
        {
            System.out.println("File not found");
        }
        catch(IOException ex)
        {
            System.out.println("IO exception occured");
        }
        finally
        {
            if(oos != null)
            {
                try
                {
                    oos.flush();
                    oos.close();
                }
                catch (IOException ex)
                {
                    
                    ex.printStackTrace();
                }
                
            }
            
        }
    }
}
Output :
Success

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App