ArrayList

作者: TYcom | 来源:发表于2017-07-20 06:35 被阅读3次

需实现序列化: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;

}

}

}

輸出結果:

相关文章

网友评论

      本文标题:ArrayList

      本文链接:https://www.haomeiwen.com/subject/flxdkxtx.html