测试示例:
1 public void testSerialize(){ 2 Person person = new Person(); 3 person.setName("LULU"); 4 person.setSex("bitch"); 5 person.setAddress("北京海淀"); 6 person.setAge(20); 7 person.setTel("1231312"); 8 person.setEducation("小学"); 9 String str=serializeObject(person); 10 Person newP= (Person) deSerializeObject(str); 11 if(newP!=null){ 12 String s=newP.toString(); 13 } 14 User user=new User("192.168.0.30",8080); 15 String userStr=serializeObject(user); 16 User deUser= (User) deSerializeObject(userStr); 17 int p=deUser.query_port; 18 List<User> users=new ArrayList<>();//序列化集合,只需要集合类的数据可以被序列化就行,序列化的方法跟其他的类序列化的方法一样 19 users.add(user); 20 String l=serializeObject(users); 21 List<User> newL= (List<User>) deSerializeObject(l); 22 newL.toString(); 23 }
1 public class User implements Serializable { 2 public String query_ip; 3 public int query_port; 4 public User(String ip,int p){ 5 query_ip=ip; 6 query_port=p; 7 } 8 }
1 public class Person implements Serializable { 2 /** 3 * 4 */ 5 private static final long serialVersionUID = 1L; 6 String name; 7 int age; 8 String address; 9 String education; 10 String tel; 11 12 public int getAge() { 13 return age; 14 } 15 16 public void setAge(int age) { 17 this.age = age; 18 } 19 20 public String getAddress() { 21 return address; 22 } 23 24 public void setAddress(String address) { 25 this.address = address; 26 } 27 28 public String getEducation() { 29 return education; 30 } 31 32 public void setEducation(String education) { 33 this.education = education; 34 } 35 36 public String getTel() { 37 return tel; 38 } 39 40 public void setTel(String tel) { 41 this.tel = tel; 42 } 43 44 public String getName() { 45 return name; 46 } 47 48 public void setName(String name) { 49 this.name = name; 50 } 51 52 public String getSex() { 53 return sex; 54 } 55 56 public void setSex(String sex) { 57 this.sex = sex; 58 } 59 60 String sex; 61 62 @Override 63 public String toString() { 64 return "Person [name=" + name + ", age=" + age + ", address=" + address 65 + ", education=" + education + ", tel=" + tel + ", sex=" + sex 66 + "]"; 67 } 68 }
转载于:https://www.cnblogs.com/xushihai/p/4957692.html
