Saving and restoring the state of classes : Object Serialization « File « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » File » Object Serialization 




import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

abstract class Shape implements Serializable {
  public static final int RED = 1, BLUE = 2, GREEN = 3;

  private int x, y, size;

  private static Random r = new Random();

  private static int counter = 0;

  public abstract void setColor(int newColor);

  public abstract int getColor();

  public Shape(int xVal, int yVal, int dim) {
    x = xVal;
    y = yVal;
    size = dim;
  }

  public String toString() {
    return getClass() "color[" + getColor() "] xPos[" + x + "] yPos[" + y + "] dim[" + size
        "]\n";
  }

}

class Circle extends Shape {
  private static int color = RED;

  public Circle(int xVal, int yVal, int dim) {
    super(xVal, yVal, dim);
  }

  public void setColor(int newColor) {
    color = newColor;
  }

  public int getColor() {
    return color;
  }
}

class Square extends Shape {
  private static int color;

  public Square(int xVal, int yVal, int dim) {
    super(xVal, yVal, dim);
    color = RED;
  }

  public void setColor(int newColor) {
    color = newColor;
  }

  public int getColor() {
    return color;
  }
}

class Line extends Shape {
  private static int color = RED;

  public static void serializeStaticState(ObjectOutputStream osthrows IOException {
    os.writeInt(color);
  }

  public static void deserializeStaticState(ObjectInputStream osthrows IOException {
    color = os.readInt();
  }

  public Line(int xVal, int yVal, int dim) {
    super(xVal, yVal, dim);
  }

  public void setColor(int newColor) {
    color = newColor;
  }

  public int getColor() {
    return color;
  }
}

public class MainClass {
  public static void main(String[] argsthrows Exception {
    List shapeTypes, shapes;
    if (args.length == 0) {
      shapeTypes = new ArrayList();
      shapes = new ArrayList();

      shapeTypes.add(Circle.class);
      shapeTypes.add(Square.class);
      shapeTypes.add(Line.class);

      shapes.add(new Square(43200));
      shapes.add(new Circle(12100));
      shapes.add(new Line(12100));

      ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("CADState.out"));
      out.writeObject(shapeTypes);
      Line.serializeStaticState(out);
      out.writeObject(shapes);
    else {
      ObjectInputStream in = new ObjectInputStream(new FileInputStream(args[0]));
      shapeTypes = (Listin.readObject();
      Line.deserializeStaticState(in);
      shapes = (Listin.readObject();
    }

    System.out.println(shapes);
  }

}
/*
*/
[class Squarecolor[1] xPos[4] yPos[3] dim[200]
, class Circlecolor[1] xPos[1] yPos[2] dim[100]
, class Linecolor[1] xPos[1] yPos[2] dim[100]
]














11.38.Object Serialization
11.38.1.Conditions for Serialization
11.38.2.Storing Objects in a File
11.38.3.Reading an Object From a File
11.38.4.Serializing Variations on an Object
11.38.5.Writing objects sequentially to a file with class ObjectOutputStream
11.38.6.Read a file of objects sequentially and displays each record
11.38.7.Only parent class is Serializable
11.38.8.Saving and restoring the state of classes
11.38.9.Class combination Serialization
11.38.10.Controlling serialization by adding your own
11.38.11.Serialization with ObjectInputStream and ObjectOutputStream
11.38.12.Deal with transient in Object Serialization
11.38.13.Serialization Utilities
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.