当前位置: 技术问答>java相关
如何让一个Vector类型serializable?
来源: 互联网 发布时间:2015-05-06
本文导语: ejb中客户端和服务器端传递参数时需要将参数实现java.io.Serializable。Vector.elements()这个函数却没有实现,有没有函数可以实现Vector的Serializable呢?据说这里汇集了中国最好的程序员,可是我提出的问题为什么老是没有...
ejb中客户端和服务器端传递参数时需要将参数实现java.io.Serializable。Vector.elements()这个函数却没有实现,有没有函数可以实现Vector的Serializable呢?据说这里汇集了中国最好的程序员,可是我提出的问题为什么老是没有人回?是不是我提的太简单,不屑于回答啊?
|
其实对象的序列化就是对象的保存(我认为)
下面有两个程序,一个是保存一个对象,一个是读出这个对象
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");
Vector 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 Vector();
repaint();
}
};
MouseAdapter drawL = new MouseAdapter(){
public void mouseClicked(MouseEvent e){
Point p = new Point(e.getX(),e.getY());
picture.(p);
repaint();
}
};
public void init(){
picture = new Vector();
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");
Vector 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 Vector();
repaint();
}
};
MouseAdapter drawL = new MouseAdapter(){
public void mouseClicked(MouseEvent e){
Point p = new Point(e.getX(),e.getY());
picture.(p);
repaint();
}
};
public void init(){
picture = new Vector();
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