Working with Serialization : Serialization « File Input Output « Java






Working with Serialization

      
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PersisTest extends JFrame implements ActionListener {

  ArrayList displayList = new ArrayList();;

  String pathname;

  JButton clearBtn, saveBtn, restoreBtn, quitBtn;

  public static void main(String args[]) {
    if (args.length == 0) {
      System.err.println("Usage: java PersisTest filename");
      System.exit(-1);
    }
    new PersisTest(args[0]).show();
  }

  public PersisTest(String pathname) {
    super("Save Me");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.pathname = pathname;
    // Build the GUI. Make this object a listener for all actions.
    JPanel pan = new JPanel();
    clearBtn = new JButton("Clear");
    clearBtn.addActionListener(this);
    pan.add(clearBtn);
    saveBtn = new JButton("Save");
    saveBtn.addActionListener(this);
    pan.add(saveBtn);
    restoreBtn = new JButton("Restore");
    restoreBtn.addActionListener(this);
    pan.add(restoreBtn);
    quitBtn = new JButton("Quit");
    quitBtn.addActionListener(this);
    pan.add(quitBtn);
    Container c = getContentPane();
    c.add(pan, BorderLayout.NORTH);
    c.add(new ClickPanel(), BorderLayout.CENTER);
    setSize(350, 200);
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == clearBtn) {
      // Repaint with an empty display list.
      displayList = new ArrayList();
      repaint();
    } else if (e.getSource() == saveBtn) {
      // Write display list array list to an object output stream.
      try {
        FileOutputStream fos = new FileOutputStream(pathname);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(displayList);
        oos.flush();
        oos.close();
        fos.close();
      } catch (IOException ex) {
        System.err.println("Trouble writing display list array list");
      }
    } else if (e.getSource() == restoreBtn) {
      // Read a new display list array list from an object input stream.
      try {
        FileInputStream fis = new FileInputStream(pathname);
        ObjectInputStream ois = new ObjectInputStream(fis);
        displayList = (ArrayList) (ois.readObject());
        ois.close();
        fis.close();
        repaint();
      } catch (ClassNotFoundException ex) {
        System.err.println("Trouble reading display list array list");
      } catch (IOException ex) {
        System.err.println("Trouble reading display list array list");
      }
    } else if (e.getSource() == quitBtn) {
      System.exit(0);
    }
  }

  class ClickPanel extends JPanel {

    ClickPanel() {
      MouseListener listener = new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
          // Store x and y in display list array list.
          Point p = new Point(e.getX(), e.getY());
          displayList.add(p);
        }

        public void mouseReleased(MouseEvent e) {
          // Store x and y in display list array list, and request
          // repaint.
          Point p = new Point(e.getX(), e.getY());
          displayList.add(p);
          repaint();
        }
      };
      addMouseListener(listener);
    }

    public void paintComponent(Graphics g) {
      // Clear to white.
      g.setColor(Color.white);
      g.fillRect(0, 0, getSize().width, getSize().height);
      // Traverse display list, drawing 1 rect for each 2 points
      // in the array list.
      g.setColor(Color.black);
      int i = 0;
      while (i < displayList.size()) {
        Point p0 = (Point) (displayList.get(i++));
        Point p1 = (Point) (displayList.get(i++));
        int x = Math.min(p0.x, p1.x);
        int y = Math.min(p0.y, p1.y);
        int w = Math.abs(p0.x - p1.x);
        int h = Math.abs(p0.y - p1.y);
        g.drawRect(x, y, w, h);
      }
    }
  }
}
           
         
    
    
    
    
    
  








Related examples in the same category

1.Serialization with ObjectInputStream and ObjectOutputStream
2.Object SerializationObject Serialization
3.Serial Demo
4.Create a serialized output file
5.Reconstructing an externalizable objectReconstructing an externalizable object
6.Simple use of Externalizable and a pitfallSimple use of Externalizable and a pitfall
7.Serializable
8.Serializer class
9.This program shows how to use getSerialVersionUID
10.Assists with the serialization process and performs additional functionality based on serialization.
11.Computes all the class serialVersionUIDs under the jboss home directory.
12.Serializable Enumeration
13.Serialization Utilities
14.This program demonstrates the transfer of serialized objects between virtual machines
15.A class whose clone method uses serialization
16.Writes a serialized version of obj to a given file, compressing it using gzip.
17.Reads a serialized object from a file that has been compressed using gzip.
18.Serialized File Util
19.Serializes an object to a file, masking out annoying exceptions.