1 /** 2 * 序列化对象 3 * 4 * @return 序列化后的字符串 5 */ 6 private String serializeObject(Object object) { 7 ByteArrayOutputStream byteArrayOutputStream=null; 8 ObjectOutputStream objectOutputStream=null; 9 String serStr=""; 10 try{ 11 byteArrayOutputStream = new ByteArrayOutputStream(); 12 objectOutputStream = new ObjectOutputStream( 13 byteArrayOutputStream); 14 objectOutputStream.writeObject(object); 15 serStr = byteArrayOutputStream.toString("ISO-8859-1"); 16 serStr = java.net.URLEncoder.encode(serStr, "UTF-8"); 17 }catch (IOException ex){ 18 19 }finally { 20 if(objectOutputStream!=null) 21 try { 22 objectOutputStream.close(); 23 } catch (IOException e) { 24 e.printStackTrace(); 25 } 26 if(byteArrayOutputStream!=null) 27 try { 28 byteArrayOutputStream.close(); 29 } catch (IOException e) { 30 e.printStackTrace(); 31 } 32 return serStr; 33 } 34 } 35 36 /** 37 * 反序列化对象 38 * 39 * @param str 40 * @return 反序列化后的对象 41 */ 42 private Object deSerializeObject(String str){ 43 String redStr = null; 44 try { 45 redStr = URLDecoder.decode(str, "UTF-8"); 46 } catch (UnsupportedEncodingException e) { 47 e.printStackTrace(); 48 } 49 ByteArrayInputStream byteArrayInputStream=null; 50 ObjectInputStream objectInputStream=null; 51 Object object=null; 52 try{ 53 byteArrayInputStream = new ByteArrayInputStream( 54 redStr.getBytes("ISO-8859-1")); 55 objectInputStream = new ObjectInputStream( 56 byteArrayInputStream); 57 object=objectInputStream.readObject(); 58 }catch (Exception ex){ 59 60 }finally { 61 if(objectInputStream!=null) 62 try { 63 objectInputStream.close(); 64 } catch (IOException e) { 65 e.printStackTrace(); 66 } 67 if(byteArrayInputStream!=null) 68 try { 69 byteArrayInputStream.close(); 70 } catch (IOException e) { 71 e.printStackTrace(); 72 } 73 } 74 return object; 75 }
测试示例:
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 }
时间: 2024-10-28 16:16:11