Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1063415
  • 博文数量: 284
  • 博客积分: 8223
  • 博客等级: 中将
  • 技术积分: 3188
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-01 13:26
文章分类

全部博文(284)

文章存档

2012年(18)

2011年(33)

2010年(83)

2009年(147)

2008年(3)

分类: Java

2010-07-27 14:12:11

import java.io.*;

class MyClass implements Serializable{
    String a;
    int b;
    double c;
    MyClass(String a, int b, double c){
        this.a = a;
        this.b = b;
        this.c = c;
    }
    public String toString(){
        return "a="+a+";b="+b+";c="+c;
    }
};
public class test {

    /**
     * @param args
     */

    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub

        try{
            MyClass object1 = new MyClass("Hello", -7, 2.7);
            FileOutputStream fos = new FileOutputStream("serial");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(object1);
            oos.close();
        }catch(Exception e){
            System.out.println("err");
        }
        
        try{
            MyClass object2;
            FileInputStream fis = new FileInputStream("serial");
            ObjectInputStream ois = new ObjectInputStream(fis);
            object2 = (MyClass)ois.readObject();
            ois.close();
            System.out.println("Object2:"+object2);
        }catch(Exception e){
            System.out.println("err");
        }
    }
    
}


阅读(1227) | 评论(0) | 转发(0) |
0

上一篇:java-输入输出流

下一篇:java-数据报

给主人留下些什么吧!~~