导航

JAVA 序列化和反序列化的用法和示例

Posted on 2010-01-15 15:57  ccjvl  阅读(470)  评论(0编辑  收藏  举报

序列化: 假设被序列化的对象有一些到其它对象的引用,这些对象还义勇了另外一些对象,在序列化该对象的时候,所有它引用的对象将被递归地定位和序列化。

序列化是把一个对象的状态写到字节流的过程。

归纳:•对象--》对象输出流--》输出流--》(序列化)
      •输入流--》对象输入流--》对象---》(反序列化)

示例如下:

1:作出一个要序列化的窗体:如图

 

类为kankan.java

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Rectangle;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;

public class kankan extends JFrame {

private static final long serialVersionUID = 1L;

private JPanel jContentPane = null;

private JRadioButton jRadioButton = null;

private JRadioButton jRadioButton1 = null;

private JTextArea jTextArea = null;

/**
* This is the default constructor
*/
public kankan() {
   super();
   initialize();
}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
   this.setSize(300, 200);
   this.setContentPane(getJContentPane());
   this.setTitle("JFrame");
}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
   if (jContentPane == null) {
    jContentPane = new JPanel();
    jContentPane.setLayout(null);
    jContentPane.add(getJRadioButton(), null);
    jContentPane.add(getJRadioButton1(), null);
    jContentPane.add(getJTextArea(), null);
   }
   return jContentPane;
}

/**
* This method initializes jRadioButton
*
* @return javax.swing.JRadioButton
*/
private JRadioButton getJRadioButton() {
   if (jRadioButton == null) {
    jRadioButton = new JRadioButton();
    jRadioButton.setBounds(new Rectangle(30, 24, 77, 21));
    jRadioButton.setText("123");
   }
   return jRadioButton;
}

/**
* This method initializes jRadioButton1
*
* @return javax.swing.JRadioButton
*/
private JRadioButton getJRadioButton1() {
   if (jRadioButton1 == null) {
    jRadioButton1 = new JRadioButton();
    jRadioButton1.setBounds(new Rectangle(35, 56, 59, 21));
    jRadioButton1.setText("1234");
   }
   return jRadioButton1;
}

/**
* This method initializes jTextArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getJTextArea() {
   if (jTextArea == null) {
    jTextArea = new JTextArea();
    jTextArea.setBounds(new Rectangle(128, 15, 109, 116));
   }
   return jTextArea;
}

}

然后在用另一窗体来调用:

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.io.*;

public class haohao extends JFrame {

private static final long serialVersionUID = 1L;

private JPanel jContentPane = null;

private JButton jButton = null;

private JButton jButton1 = null;

private JButton jButton2 = null;

private JButton jButton3 = null;
public kankan f1;
public kankan f2;

/**
* This is the default constructor
*/
public haohao() {
   super();
   initialize();
}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
   this.setSize(300, 200);
   this.setContentPane(getJContentPane());
   this.setTitle("JFrame");
}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
   if (jContentPane == null) {
    jContentPane = new JPanel();
    jContentPane.setLayout(null);
    jContentPane.add(getJButton(), null);
    jContentPane.add(getJButton1(), null);
    jContentPane.add(getJButton2(), null);
    jContentPane.add(getJButton3(), null);
   }
   return jContentPane;
}

/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
   if (jButton == null) {
    jButton = new JButton();
    jButton.setBounds(new Rectangle(14, 18, 81, 34));
    jButton.setText("f1");
    jButton.addActionListener(new java.awt.event.ActionListener() {  
     public void actionPerformed(java.awt.event.ActionEvent e) {   
                      f1=new kankan();//显示要序列化窗体
                      f1.setVisible(true);
     }
   
    });
   }
   return jButton;
}

/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
   if (jButton1 == null) {
    jButton1 = new JButton();
    jButton1.setBounds(new Rectangle(154, 20, 100, 36));
    jButton1.setText("序列化");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
     public void actionPerformed(java.awt.event.ActionEvent e) {
      f1=new kankan();
                      FileOutputStream fos = null;
       try {
        fos = new FileOutputStream ("d:\\456");//文件输出流
       } catch (FileNotFoundException e1) {
        // TODO 自动生成 catch 块
        e1.printStackTrace();
       }
                      try {
        ObjectOutputStream oos=new ObjectOutputStream(fos);//输出流的对象
        oos.writeObject(f1);//把对象写入文件流当中
        fos.close();
        oos.close();
       } catch (IOException e1) {
        // TODO 自动生成 catch 块
        e1.printStackTrace();
       }
     }
    });
   }
   return jButton1;
}

/**
* This method initializes jButton2
*
* @return javax.swing.JButton
*/
private JButton getJButton2() {
   if (jButton2 == null) {
    jButton2 = new JButton();
    jButton2.setBounds(new Rectangle(13, 100, 86, 34));
    jButton2.setText("反序列化");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
     public void actionPerformed(java.awt.event.ActionEvent e) {
              
                FileInputStream fis = null;
     try {
      fis = new FileInputStream("d:\\123");//文件输入流
     } catch (FileNotFoundException e1) {
      // TODO 自动生成 catch 块
      e1.printStackTrace();
     }
                try {
      ObjectInputStream ois=new ObjectInputStream(fis);//创建文件输入流的对象
      f2=(kankan) ois.readObject();//把文件流读取出来给对象
      fis.close();
      ois.close();
     
     } catch (IOException e1) {
      // TODO 自动生成 catch 块
      e1.printStackTrace();
     } catch (ClassNotFoundException e2) {
      // TODO 自动生成 catch 块
      e2.printStackTrace();
     }
             
               
     }
    });
   }
   return jButton2;
}

/**
* This method initializes jButton3
*
* @return javax.swing.JButton
*/
private JButton getJButton3() {
   if (jButton3 == null) {
    jButton3 = new JButton();
    jButton3.setBounds(new Rectangle(158, 96, 96, 39));
    jButton3.setText("F2");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
     public void actionPerformed(java.awt.event.ActionEvent e) {
                    f2=new kankan();
                    f2.setVisible(true);
     }
    });
   }
   return jButton3;
}

}

 

当你想要把对象状态保存到一个永久存储区域(例如一个文件)中时,就会用到它。以后,你可以通过反序列化的过程来还原它。
•序列化也是实现远呈方法调用所需的,对象可以作为远程参数被传递。发送方序列化该对象,然后传送它,接收方再通过反序列化还原对象。