需实现序列化:ObjectOutputStream,ObjectInputStream分别写对象到存储介质,读对象到内存;
transant修饰的变量表示不进行序列化;
public class TestSerializable implements Serializable{
/**
* @Fields serialVersionUID:
*/
private static final long serialVersionUID = -5259348695998145058L;
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
Test test = new TestSerializable().new Test("12", "34");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d://object.txt"));
oos.writeObject(test);
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d://object.txt"));
System.out.println((Test)ois.readObject());
ois.close();
}
class Test implements Serializable{
/**
* @Fields serialVersionUID:
*/
private static final long serialVersionUID = 2135037221437516511L;
transient String aString;
String bString;
public Test(String a, String b) {
aString = a;
bString = b;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "a=" + aString + ",b=" + bString;
}
}
}
輸出結果:
网友评论