IO//操作对象--对象的序列化

成对 使用的两个类 : ObjectInputStream  ObjectOutputStream   


import
java.io.*; import java.util.*; public class Practice_2 { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub writeObj(); //readObj(); } public static void writeObj() throws IOException { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt")); oos.writeObject(new Person("lisi",39));; oos.close(); } } class Person implements Serializable { public static final long serialVersionUID = 42L; //对象系统会给序列化编码,但当原对象被删除,再产生就是新的。 //运行会产生问题,自定义UID编码会解决这个问题。 String name; transient int age; //被transient修饰、不能被序列化。 static String country = "cn"; //静态不能被序列化 Person(String name,int age) { this.name = name; this.age = age; } public String toString() { return name+":"+age+":"+country; } }

 

posted @ 2019-11-07 10:58  蚂蚁雅黑1010  阅读(160)  评论(0编辑  收藏  举报