Java_IO_对象流ObjectOutputStream&ObjectInputStream

package cn.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

/**
 * 序列化:对象流 写到字节数组
 * 写出后读取,读取与写出保持顺序一致,不是所有对象都可以序列化,需实现Serializable接口
 * @author Chill Lyn
 *
 */
public class TestObjectStream {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		// 序列化:写出
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(baos));
		// 操作数据类型+数据
		oos.writeBoolean(false);
		oos.writeChar('a');
		oos.writeInt(0);
		oos.writeUTF("我爱你");
		oos.writeObject("我还爱你");
		oos.writeObject(new Drink("Cola", 3, 2));
		oos.writeObject(new Date());
		oos.flush();
		byte[] datas = baos.toByteArray();
		// 反序列化:读取
		ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
		// 顺序与写出顺序一致
		boolean flag = ois.readBoolean();
		char ch = ois.readChar();
		int i = ois.readInt();
		String str = ois.readUTF();
		Object str2 = ois.readObject();
		Object drink = ois.readObject();
		Object date = ois.readObject();

		if (str2 instanceof String) {
			String strObj = (String) str2;
			System.out.println(strObj);
		}

		if (drink instanceof Drink) {
			Drink drinkObj = (Drink) drink;
			System.out.println(drinkObj.getName() + "-->" + drinkObj.getPrice() + "-->" + drinkObj.getCost());
		}

		if (date instanceof Date) {
			Date dateObj = (Date) date;
			System.out.println(dateObj);
		}
		System.out.println(ch);
	}
}

class Drink implements java.io.Serializable {
	private String name;
	private double price;
	private transient double cost; // 该数据不需要序列化

	public Drink() {

	}

	public Drink(String name, double price, double cost) {
		super();
		this.name = name;
		this.price = price;
		this.cost = cost;
	}

	public String getName() {
		return name;
	}

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

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public double getCost() {
		return cost;
	}

	public void setCost(double cost) {
		this.cost = cost;
	}
}
package cn.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

/**
 * 对象流 写到文件
 * 写出后读取,读取与写出保持顺序一致,不是所有对象都可以序列化,需实现Serializable接口
 * @author Chill Lyn
 *
 */
public class TestObjectStream2 {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		// 序列化:写出
		ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("obj.txt")));
		// 操作数据类型+数据
		oos.writeBoolean(false);
		oos.writeChar('a');
		oos.writeInt(0);
		oos.writeUTF("我爱你");
		oos.writeObject("我还爱你");
		oos.writeObject(new Drink("Cola", 3, 2));
		oos.writeObject(new Date());
		oos.flush();
		oos.close();
		// 反序列化:读取
		ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("obj.txt")));
		// 顺序与写出顺序一致
		boolean flag = ois.readBoolean();
		char ch = ois.readChar();
		int i = ois.readInt();
		String str = ois.readUTF();
		Object str2 = ois.readObject();
		Object drink = ois.readObject();
		Object date = ois.readObject();

		if (str2 instanceof String) {
			String strObj = (String) str2;
			System.out.println(strObj);
		}

		if (drink instanceof Drink) {
			Drink drinkObj = (Drink) drink;
			System.out.println(drinkObj.getName() + "-->" + drinkObj.getPrice() + "-->" + drinkObj.getCost());
		}

		if (date instanceof Date) {
			Date dateObj = (Date) date;
			System.out.println(dateObj);
		}
		System.out.println(ch);

		ois.close();
	}
}

备注
1.可以看作数据流的升级版,可以操作引用类型数据。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值