转载

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 }

结果显示 Java反序列化测试

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反序列化测试

注意:

·实体类必须实现序列化接口“ java.io.Serializable

·生成的obj.object 因为是二进制文件,故无法正常打开,若notepad打开也是乱码!

Java反序列化测试

总结:序列化技术在web端技术的应用相当重要,希望学习Java的朋友都能理解该技术并进行应用。

正文到此结束
Loading...