Reading Basic Data From an Object Stream : ObjectOutputStream « File « Java Tutorial






import java.awt.Polygon;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class MainClass {
  public static void main(String[] args) {
    int[] x = new int[] { 1, 2, 3 };
    int[] y = new int[] { 4, 5, 6 };
    Polygon polygon = new Polygon(x, y, x.length);
    try {
      ObjectOutputStream objectOut = new ObjectOutputStream(new BufferedOutputStream(
          new FileOutputStream("C:/Polygons.bin")));
      objectOut.writeObject(polygon);
      objectOut.close();
    } catch (IOException e) {
      e.printStackTrace(System.err);
    }
    try {
      ObjectInputStream objectIn = new ObjectInputStream(new BufferedInputStream(
          new FileInputStream("C:/Polygons.bin")));
      Polygon theLine = (Polygon) objectIn.readObject();
      System.out.println(theLine);
      objectIn.close();
    } catch (Exception e) {
      e.printStackTrace(System.err);
    }
  }
}








11.23.ObjectOutputStream
11.23.1.Create ObjectOutputStream out of FileOutputStream
11.23.2.Writing objects to file with ObjectOutputStream
11.23.3.Serializing an Object (JButton)
11.23.4.Reading Basic Data From an Object Stream
11.23.5.Create a serialized output file.
11.23.6.Try to recover a serialized file without the class of object that's stored in that file
11.23.7.Object IO
11.23.8.illustrates serialization and deserialization