当前位置: 技术问答>java相关
对“对象系列化”的概念不是很懂。请指教,最好能举例说明
来源: 互联网 发布时间:2015-05-04
本文导语: 对“对象系列化”的概念不是很懂。请指教,最好能举例说明 | 其实对象的序列化就是对象的保存(我认为) 下面有两个程序,一个是保存一个对象,一个是读出这个对象 import java.awt.*; import javax...
对“对象系列化”的概念不是很懂。请指教,最好能举例说明
|
其实对象的序列化就是对象的保存(我认为)
下面有两个程序,一个是保存一个对象,一个是读出这个对象
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2002
* Company:
* @author
* @version 1.0
*/
public class SerialSample1 extends JFrame {
public SerialSample1(String title) {
super(title);
}
public static void main(String[] args) {
SerialSample1 frame1 = new SerialSample1("SerialSample1");
frame1.init();
frame1.setSize(400,400);
frame1.setVisible(true);
frame1.addWindowListener(frame1.a);
}
WindowClose a = new WindowClose();
class WindowClose extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
DrawPicture drawArea = new DrawPicture();
JButton
saveButton = new JButton("Save Picture"),
clearButton = new JButton("Clear Picture");
ArrayList picture;
ActionListener saveL = new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("objectFile"));
out.writeObject(picture);
out.flush();
out.close();
}catch(Exception ex){
}
}
};
ActionListener clearL = new ActionListener(){
public void actionPerformed(ActionEvent e){
picture = new ArrayList();
repaint();
}
};
MouseAdapter drawL = new MouseAdapter(){
public void mouseClicked(MouseEvent e){
Point p = new Point(e.getX(),e.getY());
picture.add(p);
repaint();
}
};
public void init(){
picture = new ArrayList();
saveButton.addActionListener(saveL);
clearButton.addActionListener(clearL);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(saveButton);
cp.add(clearButton);
cp.add(drawArea);
File file = new File("c:"+System.getProperty("file.separator")+"dlr.txt");
System.out.println(file.getPath());
System.out.println(file.exists());
drawArea.addMouseListener(drawL);
}
class DrawPicture extends JTextArea{
DrawPicture(){
super("Draw circles",10,20);
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.blue);
int i=0;
while(i
下面有两个程序,一个是保存一个对象,一个是读出这个对象
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2002
* Company:
* @author
* @version 1.0
*/
public class SerialSample1 extends JFrame {
public SerialSample1(String title) {
super(title);
}
public static void main(String[] args) {
SerialSample1 frame1 = new SerialSample1("SerialSample1");
frame1.init();
frame1.setSize(400,400);
frame1.setVisible(true);
frame1.addWindowListener(frame1.a);
}
WindowClose a = new WindowClose();
class WindowClose extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
DrawPicture drawArea = new DrawPicture();
JButton
saveButton = new JButton("Save Picture"),
clearButton = new JButton("Clear Picture");
ArrayList picture;
ActionListener saveL = new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("objectFile"));
out.writeObject(picture);
out.flush();
out.close();
}catch(Exception ex){
}
}
};
ActionListener clearL = new ActionListener(){
public void actionPerformed(ActionEvent e){
picture = new ArrayList();
repaint();
}
};
MouseAdapter drawL = new MouseAdapter(){
public void mouseClicked(MouseEvent e){
Point p = new Point(e.getX(),e.getY());
picture.add(p);
repaint();
}
};
public void init(){
picture = new ArrayList();
saveButton.addActionListener(saveL);
clearButton.addActionListener(clearL);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(saveButton);
cp.add(clearButton);
cp.add(drawArea);
File file = new File("c:"+System.getProperty("file.separator")+"dlr.txt");
System.out.println(file.getPath());
System.out.println(file.exists());
drawArea.addMouseListener(drawL);
}
class DrawPicture extends JTextArea{
DrawPicture(){
super("Draw circles",10,20);
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.blue);
int i=0;
while(i