In this tutorial we will discuss about the Java class ObjectOutputStream.
In this tutorial we will discuss about the Java class ObjectOutputStream.In this tutorial we will discuss about the Java class ObjectOutputStream.
java.io.ObjectOutputStream class is used to write the primitive data types and the graphs of Java objects to the output stream. It writes only those objects to the stream which implements the java.io.Serializable interface. To write the primitive data types to the stream methods of DataOutput can be used. writeObject method can write any object to the stream whether the objects are String or arrays. To read the objects written into the output stream a corresponding ObjectInputStream class is used with the same types and order in which they are written.
Constructors of ObjectOutuptStream
ObjectOutputStream() | This is a default constructor which is implemented by the subclasses in a
way to not have to allocate private data. Syntax : protected ObjectOutputStream() throws IOException, SecurityException |
ObjectOutputStream(OutputStream out) | This is a parameterized constructor which is used to create an object of
ObjectOutputStream that contains the reference of OutputStream where the data to
be written. Syntax :public ObjectOutputStream(OutputStream out) throws IOException |
Methods of ObjectOutputStream
Commonly used methods of ObjectOutputStream
Example
An example is being given here which will demonstrate you about how to use the ObjectOutuptStream class to write the object to the output stream. In this example at first I have created a Java class which writes the objects to the output stream then created a corresponding Java class which will read the objects from the stream.
Source Code
ObjectOutputStreamExample.java
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class ObjectOutputStreamExample { public static void main(String args[]) { ObjectOutputStream oos = null; try { FileOutputStream fos = new FileOutputStream("employee.txt"); oos = new ObjectOutputStream(fos); oos.writeObject("Shashi"); oos.writeObject("Content Writer"); oos.writeInt(20000); oos.writeObject("Amit"); oos.writeObject("Content Writer"); oos.writeInt(18000); System.out.println(); System.out.println("objects written successfully"); } catch (FileNotFoundException fex) { System.out.println(fex); } catch (Exception ex) { System.out.println(ex); } finally { try { if (oos != null) { oos.close(); } } catch (Exception ex1) { System.out.println(ex1); } } } }
ReadObjectInputStream.java
import java.io.*; public class ReadObjectInputStream { public static void main(String args[]) { ObjectInputStream ois = null; try { FileInputStream fis = new FileInputStream("employee.txt"); ois = new ObjectInputStream(fis); String sh = (String) ois.readObject(); String cw = (String) ois.readObject(); int sal = ois.readInt(); String am = (String) ois.readObject(); String cw1 = (String) ois.readObject(); int sal1 = ois.readInt(); System.out.println(); System.out.println("Name \t"+"Department \t"+"Salary"); System.out.println(sh+"\t"+cw+"\t"+sal); System.out.println(am+"\t"+cw1+"\t"+sal1); } catch (EOFException ex) { //This exception will be caught when EOF is reached System.out.println("End of file reached."); } catch (ClassNotFoundException ex) { System.out.println(ex); } catch (Exception ex) { System.out.println(ex); } finally { try { if (ois != null) { ois.close(); } } catch (Exception ex) { System.out.println(ex); } } } }
Output
First compile and execute the ObjectOutputStreamExample.java which will write the objects to the output stream as follows :
Then Compile and execute the ReadObjectInputStream.java which will read the objects written to the output stream then the output will be as follows :
Ads