序列化与反序列化

来源:互联网 发布:java se 5.0下载 编辑:程序博客网 时间:2024/06/06 08:56

1.序列化:指将对象保存到硬盘中

2.反序列化:将存档从硬盘中读出来

3.序列化:

1.首先要实例化一个对象:

Person person=new Person("admin", "123");

2.使用io流将对象写入硬盘中:

确认要写在硬盘的何处(path

FileOutputStream fos=new FileOutputStream(path);

ObjectOutputStream oos=new ObjectOutputStream(fos);

3.使用writeObject()方法进行写的操作

oos.writeObject(person);

4.关闭流:

oos.close();

fos.close();

 4.反序列化

1.找到要反序列化的文件(path

2.进行读取:

FileInputStream fis=new FileInputStream("user.exe");

ObjectInputStream ois=new ObjectInputStream(fis);

3.读取对象,且用对象进行接收

Person person2=(Person) ois.readObject();

4.关闭流

ois.close();

fis.close();

原创粉丝点击