Object流

                Object流:直接将Object写入或读入、transient关键字、serializable接口、externalizable接口(用于控制序列化过程)
import java.io.*;

public class TestObjectIO {
	public static void main(String args[]) throws Exception {
		T t = new T();
		t.k = 8;
		FileOutputStream fos = new FileOutputStream(
				"d:/share/java/io/testobjectio.dat");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		//套上ObjectOutputStream,专门用于读写Object
		oos.writeObject(t);
		oos.flush();
		oos.close();

		FileInputStream fis = new FileInputStream(
				"d:/share/java/io/testobjectio.dat");
		ObjectInputStream ois = new ObjectInputStream(fis);
		//读出object类型
		T tReaded = (T) ois.readObject();//强制转换为T类型
		System.out.println(tReaded.i + " " + tReaded.j + " " + tReaded.d + " "
				+ tReaded.k);

	}
}

@SuppressWarnings("serial")
class T implements Serializable {  //标记可以被序列化(字符/节流)
	int i = 10;
	int j = 9;
	double d = 2.3;
	transient int k = 15;//transient 透明的,可以用来修饰成员变量
	                    //在序列化时不予考虑,所以再读出时为默认值0
}

你可能感兴趣的:(java,J#)