Working with Serialization : Serialization « File Input Output « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » File Input Output » SerializationScreenshots 
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(350200);
  }

  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(00, 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. Object SerializationObject Serialization
2. Serial Demo
3. Create a serialized output file
4. Reconstructing an externalizable objectReconstructing an externalizable object
5. Simple use of Externalizable and a pitfallSimple use of Externalizable and a pitfall
6. Serializable
7. Serializer class
8. This program shows how to use getSerialVersionUID
w___w___w_.___j__a__v__a__2___s___.c__o___m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.