java对象的序列化和反序列化

Posted 薄灬荷

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java对象的序列化和反序列化相关的知识,希望对你有一定的参考价值。

 1 package com.lovo.test;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 import java.io.ObjectInputStream;
 8 import java.io.ObjectOutputStream;
 9 import java.util.ArrayList;
10 
11 import com.lovo.bean.Student;
12 
13 public class TestObject {
14 
15     public static void main(String[] args) {
16         // TODO Auto-generated method stub
17         //对象的序列化--将对象以二进制的形式输出(没有确定输出到哪儿去)
18 //        ArrayList<Student> al = new ArrayList<Student>();
19 //        Student stu0 = new Student("HJ", 25,"四川","成都","九眼桥第三桥洞");
20 //        Student stu1 = new Student("YP", 27,"四川","成都","九眼桥第2桥洞");
21 //        al.add(stu0);
22 //        al.add(stu1);
23 //        
24 //        ObjectOutputStream oos = null;
25 //        
26 //        try {
27 //            oos = new ObjectOutputStream(new FileOutputStream("student.data"));
28 //            oos.writeObject(al);
29 //        } catch (FileNotFoundException e) {
30 //            // TODO Auto-generated catch block
31 //            e.printStackTrace();
32 //        } catch (IOException e) {
33 //            // TODO Auto-generated catch block
34 //            e.printStackTrace();
35 //        }finally{
36 //            if(oos != null){
37 //                try {
38 //                    oos.close();
39 //                } catch (IOException e) {
40 //                    // TODO Auto-generated catch block
41 //                    e.printStackTrace();
42 //                }
43 //            }
44 //        }
45         
46         
47         //反序列化--将输入的二进制流转换为对象(同样不确定二进制流的数据源)
48         ArrayList<Student> al = null;
49         ObjectInputStream ois = null;
50         
51         try {
52             //使用了装饰器模式
53             ois = new ObjectInputStream(new FileInputStream("student.data"));        
54             al = (ArrayList<Student>)ois.readObject();
55         } catch (FileNotFoundException e) {
56             // TODO Auto-generated catch block
57             e.printStackTrace();
58         } catch (IOException e) {
59             // TODO Auto-generated catch block
60             e.printStackTrace();
61         } catch (ClassNotFoundException e) {
62             // TODO Auto-generated catch block
63             e.printStackTrace();
64         } finally{
65             if(ois != null){
66                 try {
67                     ois.close();
68                 } catch (IOException e) {
69                     // TODO Auto-generated catch block
70                     e.printStackTrace();
71                 }
72             }
73         }
74         
75         System.out.println(al.get(0));
76         System.out.println(al.get(1));
77     }
78 
79 }

对象

 1 package com.lovo.bean;
 2 
 3 import java.io.Serializable;
 4 
 5 //Serializable接口是一个标示接口,相当于给类打上一个标志允许参与序列化和反序列化
 6 public class Student implements Serializable{
 7     
 8     private String name;
 9     
10     private int age;
11     
12     private Address address;
13     
14     public Student(){
15         
16     }
17 
18     public Student(String name, int age) {
19         super();
20         this.name = name;
21         this.age = age;
22     }
23     
24     
25 
26     public Student(String name, int age, String state,String city,String street) {
27         super();
28         this.name = name;
29         this.age = age;
30         this.address = new Address(state,city,street);
31     }
32 
33     public String getName() {
34         return name;
35     }
36 
37     public void setName(String name) {
38         this.name = name;
39     }
40 
41     public int getAge() {
42         return age;
43     }
44 
45     public void setAge(int age) {
46         this.age = age;
47     }
48     
49     @Override
50     public String toString() {
51         // TODO Auto-generated method stub
52         return this.name + ":" + this.age + ":" + this.address;
53     }
54 }
 1 package com.lovo.bean;
 2 
 3 import java.io.Serializable;
 4 
 5 public class Address implements Serializable{
 6     
 7     private String state;
 8     
 9     private String city;
10     
11     private String street;
12     
13     public Address(){
14         
15     }
16 
17     public Address(String state, String city, String street) {
18         super();
19         this.state = state;
20         this.city = city;
21         this.street = street;
22     }
23 
24     public String getState() {
25         return state;
26     }
27 
28     public void setState(String state) {
29         this.state = state;
30     }
31 
32     public String getCity() {
33         return city;
34     }
35 
36     public void setCity(String city) {
37         this.city = city;
38     }
39 
40     public String getStreet() {
41         return street;
42     }
43 
44     public void setStreet(String street) {
45         this.street = street;
46     }
47     
48     @Override
49     public String toString() {
50         // TODO Auto-generated method stub
51         return this.state + "-" + this.city + "-" + this.street;
52     }
53 }

 

以上是关于java对象的序列化和反序列化的主要内容,如果未能解决你的问题,请参考以下文章

java反序列化引起的内存溢出怎么解决

一文带你全面了解java对象的序列化和反序列化

java序列化和反序列化

HashMap(key,Object)中的Java Gson序列化和反序列化对象[重复]

Java基础学习总结——Java对象的序列化和反序列化

Java序列化和反序列化——田少嵩