Java反序列化測試


前言:有沒有想過,如何將對象進行“加密”后寫入磁盤?序列化幫你實現!

1.概念

序列化 (Serialization)將對象的狀態信息轉換為可以存儲或傳輸的形式的過程。在序列化期間,對象將其當前狀態寫入到臨時或持久性存儲區。以后,可以通過從存儲區中讀取或反序列化對象的狀態,重新創建該對象.

2.反序列化Java實驗

--測試的實體類--

 1 package exercise;
 2 
 3 import java.io.Serializable;
 4 
 5 public class Person implements Serializable{
 6     private String name;
 7     private int age;
 8     
 9     public Person() {
10     }
11 
12     public String getName() {
13         return name;
14     }
15 
16     public void setName(String name) {
17         this.name = name;
18     }
19 
20     public int getAge() {
21         return age;
22     }
23 
24     public void setAge(int age) {
25         this.age = age;
26     }
27 
28     public Person(String name, int age) {
29         super();
30         this.name = name;
31         this.age = age;
32     }
33     
34 }

 

1)單對象序列化

 1 package exercise;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 import java.io.ObjectInputStream;
 7 import java.io.ObjectOutputStream;
 8 import java.util.ArrayList;
 9 import java.util.List;
10 
11 
12 public class ObjectStreamDemo1 {
13     /**
14      * @param args
15      * @throws IOException
16      * @throws ClassNotFoundException
17      */
18     
19     public final static String PATH = "obj.object1";
20     
21     
22     public static void main(String[] args) throws IOException,
23             ClassNotFoundException {
24         //writeObj();
25         readObj();
26         System.out.println("--End--");
27     }
28 
29     public static void readObj() throws IOException, ClassNotFoundException {
30         ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
31                 PATH));
32         
33         
34         Person p  = (Person)ois.readObject();
35         System.out.println(p.getName() + "|" + p.getAge());
36         
37     }
38 
39     public static void writeObj() throws IOException {
40         ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
41                 PATH));
42 
43         oos.writeObject(new Person("張三", 30));
44         oos.close();
45     }
46 }

結果顯示

 

2)多對象序列化

 1 package exercise;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 import java.io.ObjectInputStream;
 7 import java.io.ObjectOutputStream;
 8 import java.util.ArrayList;
 9 import java.util.List;
10 
11 
12 public class ObjectStreamDemo2 {
13     /**
14      * @param args
15      * @throws IOException
16      * @throws ClassNotFoundException
17      */
18     public final static String PATH = "obj.object";
19     public static void main(String[] args) throws IOException,
20             ClassNotFoundException {
21         
22         //writeObj();
23         readObj();
24         System.out.println("---end!---");
25     }
26 
27     public static void readObj() throws IOException, ClassNotFoundException {
28         ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
29                 PATH));
30         
31         List<Person> persons  = (List<Person>)ois.readObject();
32         for(Person p:persons){
33             System.out.println(p.getName() + "|" + p.getAge());
34         }
35     }
36 
37     public static void writeObj() throws IOException {
38         ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
39                 PATH));
40         
41         List<Person> persons = new ArrayList<Person>();
42         Person p1 = new Person("張三",18); 
43         Person p2 = new Person("李四",19); 
44         persons.add(p1);
45         persons.add(p2);
46         oos.writeObject(persons);
47         oos.close();
48     }
49 }

結果顯示

注意:

·實體類必須實現序列化接口“java.io.Serializable

·生成的obj.object 因為是二進制文件,故無法正常打開,若notepad打開也是亂碼!

 

總結:序列化技術在web端技術的應用相當重要,希望學習Java的朋友都能理解該技術並進行應用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM