Java实现序列化和反序列化

      注意事项:.对象需要连接可序列化对象: Serialzable。 代码如下:

package com.owant;

import java.io.Serializable;

public class MyObject implements Serializable {
	private String name; // 名字
	private String title;// 头衔
	private String email;// 邮件
	private int tel;     // 电话
	private String info; // 信息

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public int getTel() {
		return tel;
	}

	public void setTel(int tel) {
		this.tel = tel;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

}


对象序列化:


package com.owant;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class ObjectToStream {
	/**
	 *对象序列化:
	 *   将对象转化为二进制的文字
	 * 
	 */
	public static void main(String[] args) {
         try {
			FileOutputStream outputStream=new FileOutputStream("F:/db/object.dat");
			
			//创建一个对象
			MyObject object=new MyObject();
			object.setName("钟刘旺");
			object.setTitle("欣赏创造美好未来");
			object.setTel(1360800712);
			object.setEmail("owant.@139.com");
			object.setInfo("今天,我在研究对象序列化的问题。");
			
			ObjectOutputStream oos=new ObjectOutputStream(outputStream);
			oos.writeObject(object);
			oos.close();
			outputStream.close();
			System.out.println("写入成功!");
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}





对象反序列化:


package com.owant;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

public class ObjectFormStream {
	/**
	 *  反序列化:
	 *   将二进制的对象转化为对象
	 *    
	 */

	public static void main(String[] args) {
		try {
			FileInputStream inputStream=new FileInputStream("F:/db/object.dat");
			ObjectInputStream ois=new ObjectInputStream(inputStream);
			MyObject object=(MyObject)ois.readObject();
			ois.close();
			inputStream.close();
			
			System.out.println("读取到的对象是:\n"+
			     "name:"+object.getName()+"\n"+
			     "title:"+object.getTitle()+"\n"+
			     "tel:"+object.getTel()+"\n"+
			     "email:"+object.getEmail()+"\n"+
			     "info:"+object.getInfo()
			     );
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			System.out.println("没找到这个对象!");
			e.printStackTrace();
		}
	}

}



运行结果:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值