Java I/O 学习笔记(9) 对象持久化

package files;

import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * 数据类实现Serializable接口
 * 
 * @author QuPeng
 * 
 */
class Data1 implements Serializable {

	private static final long serialVersionUID = 1L;

	private int i = 0;

	private transient int j = 0;

	public int getI() {
		return i;
	}

	public void setI(int i) {
		this.i = i;
	}

	public int getJ() {
		return j;
	}

	public void setJ(int j) {
		this.j = j;
	}

}

/**
 * 数据类实现Externalizable接口,必须声明无参构造函数,所有需要序列化的属性必需自己实现
 * 
 * @author QuPeng
 * 
 */
class Data2 implements Externalizable {

	public static int j = 0;

	private int i = 0;

	public Data2() {
	}

	public int getI() {
		return i;
	}

	public void setI(int i) {
		this.i = i;
	}

	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		out.writeInt(j);
		out.writeInt(i);

	}

	@Override
	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		j = in.readInt();
		i = in.readInt();
	}

}

/**
 * 数据类实现Serializable接口,添加writeObject和readObject方法,用来持久化静态属性
 * 
 * @author QuPeng
 * 
 */
class Data3 implements Serializable {

	private static final long serialVersionUID = 1L;

	public static int j = 0;

	private int i = 0;

	public int getI() {
		return i;
	}

	public void setI(int i) {
		this.i = i;
	}

	private void writeObject(ObjectOutputStream stream) throws IOException {
		stream.defaultWriteObject();
		stream.writeInt(j);
	}

	private void readObject(ObjectInputStream stream) throws IOException,
			ClassNotFoundException {
		stream.defaultReadObject();
		j = stream.readInt();
	}
}

public class TestObjectStream {

	private static final long serialVersionUID = 1L;

	public static void main(String[] args) throws FileNotFoundException,
			IOException, ClassNotFoundException {
		testSerializableObject();
		testExternalizableObject();
		testSerializable2();
	}

	public static void testSerializableObject() throws FileNotFoundException,
			IOException, ClassNotFoundException {
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
				"output.txt"));
		Data1 d = new Data1();
		d.setI(99);
		oos.writeObject(d);
		oos.close();

		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
				"output.txt"));
		Data1 dRestore = (Data1) ois.readObject();
		System.out.println(dRestore.getI());
		ois.close();
	}

	public static void testExternalizableObject() throws FileNotFoundException,
			IOException, ClassNotFoundException {
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
				"output.txt"));
		Data2 d = new Data2();
		d.setI(88);
		Data2.j = 99;
		oos.writeObject(d);
		oos.close();

		Data2.j = 77;

		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
				"output.txt"));
		Data2 dRestore = (Data2) ois.readObject();
		System.out.println(Data2.j);
		System.out.println(dRestore.getI());
		ois.close();
	}

	public static void testSerializable2() throws FileNotFoundException,
			IOException, ClassNotFoundException {
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
				"output.txt"));
		Data3 d = new Data3();
		d.setI(88);
		Data3.j = 99;
		oos.writeObject(d);
		oos.close();

		Data3.j = 77;

		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
				"output.txt"));
		Data3 dRestore = (Data3) ois.readObject();
		System.out.println(Data3.j);
		System.out.println(dRestore.getI());
		ois.close();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值