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();
	}
}

你可能感兴趣的:(Java I/O 学习笔记(9) 对象持久化)