直接将Object写入或读出;
import java.io.*;
public class TestObjectIO {
public static void main(String args[]){
T t = new T();
t.k=15;
try {
FileOutputStream fos = new FileOutputStream("d:\\logs");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(t);
oos.flush();
oos.close();
FileInputStream fis = new FileInputStream("d:\\logs");
ObjectInputStream ois = new ObjectInputStream(fis);
T tReaded = (T)ois.readObject();
System.out.println(tReaded.i+" "+tReaded.j+" "+t.k);
}catch (IOException | ClassNotFoundException e){
e.printStackTrace();
}
}}
class T implements Serializable{ //可以序列化;externalizable方法可以自己控制序列化过程
int i = 10;
int j = 9;
transient double d= 2.3;//标记为透明,序列化不考虑;
int k = 15;
}