Chinaunix首页 | 论坛 | 博客
  • 博客访问: 698918
  • 博文数量: 463
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 4580
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-15 16:47
文章分类

全部博文(463)

文章存档

2011年(1)

2008年(462)

我的朋友

分类:

2008-10-15 16:52:39

import java.io.*;

import java.util.Date;

/**

* 对象的序列化和反序列化测试类.

* @author AmigoXie

* @version 1.0

* Creation date: 2007-9-15 - 下午21:45:48

*/

public class ObjectSaver {

 /**

 * @param args

 * @author AmigoXie

 * Creation date: 2007-9-15 - 下午21:45:37

 */

public static void main(String[] args) throws Exception {

 ObjectOutputStream out = new ObjectOutputStream

(new FileOutputStream("D:""objectFile.obj"));

 //序列化对象

 Customer customer = new Customer("阿蜜果", 24);

 out.writeObject("你好!");

 out.writeObject(new Date());

 out.writeObject(customer);

 out.writeInt(123); //写入基本类型数据

 out.close();

 //反序列化对象

 ObjectInputStream in = new ObjectInputStream

(new FileInputStream("D:""objectFile.obj"));

 System.out.println("obj1=" + (String) in.readObject());

 System.out.println("obj2=" + (Date) in.readObject());

 Customer obj3 = (Customer) in.readObject();

 System.out.println("obj3=" + obj3);

 int obj4 = in.readInt();

 System.out.println("obj4=" + obj4);

 in.close();

}

}

class Customer implements Serializable {

private String name;

private int age;

public Customer(String name, int age) {

this.name = name;

this.age = age;

}

public String toString() {

return "name=" + name + ", age=" + age;

}

}

--------------------next---------------------

阅读(528) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~