Serializable & transient

import java.io.*;

public class TestObjectIO {
	public static void main(String args[]) throws Exception {
		Ser se = new Ser();
		se.k = 8;
		FileOutputStream fos = new FileOutputStream("D:\\bak\\log.dat");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(se);
		oos.flush();
		oos.close();

		FileInputStream fis = new FileInputStream("D:\\bak\\log.dat");
		ObjectInputStream ois = new ObjectInputStream(fis);
		Ser sReaded = (Ser) ois.readObject();
		System.out.println(sReaded.i + " " + sReaded.j + " " + sReaded.d + " "
				+ sReaded.k);

	}
}

class Ser implements Serializable {// 把类对象序列化成字节流,把对象写入网络或者硬盘必须实现这个接口;
	int i = 10;
	int j = 9;
	double d = 2.3;
	// int k=18;
	transient int k = 18;// transient 透明的,序列化时不予考虑;
}

结果:10 9 2.3 0

备注:学习用,源自sxt.msbjava

相关文章
相关标签/搜索