java序列化和反序列化
什么是java序列化和反序列化?
序列化的原理是将一个对象转成字节流,使其能被写入到文件中,通过网络传输或者存储在数据库中。反序列化则与其相反。
简而言之,序列化就是将对象转成字节流,反序列化则是将字节流重新恢复成对象。
一个类只有实现了java.io.Serializable接口才有资格进行序列化。
定义Person类:
该类中将nationality声明为transient,所以该参数不会参与序列化操作。
1 package com.led.javabase; 2 3 import java.io.Serializable; 4 5 /** 6 * person entity 7 */ 8 public class Person implements Serializable{ 9 private String name; 10 private int age; 11 private String gender; 12 private transient String nationality;//this field will not take part in serialization 13 14 public String getName() { 15 return name; 16 } 17 18 public void setName(String name) { 19 this.name = name; 20 } 21 22 public int getAge() { 23 return age; 24 } 25 26 public void setAge(int age) { 27 this.age = age; 28 } 29 30 public String getGender() { 31 return gender; 32 } 33 34 public void setGender(String gender) { 35 this.gender = gender; 36 } 37 38 public String getNationality() { 39 return nationality; 40 } 41 42 public void setNationality(String nationality) { 43 this.nationality = nationality; 44 } 45 46 public Person(String name, int age, String gender, String nationality) { 47 this.name = name; 48 this.age = age; 49 this.gender = gender; 50 this.nationality = nationality; 51 } 52 }
序列化实例:
下面的例子是将一个person实例序列化到Person.ser文件中,通过FileOutputStream和ObjectOutputStream来进行操作的:
1 package com.led.javabase; 2 3 import java.io.*; 4 5 /** 6 * java serialization test 7 * @author Alan 8 */ 9 public class SerializationTest1 { 10 public static void main(String[] args) { 11 Person person = new Person("Jack",11,"male","US"); 12 try { 13 FileOutputStream fos = new FileOutputStream("Person.ser"); 14 ObjectOutputStream oos = new ObjectOutputStream(fos); 15 oos.writeObject(person); 16 oos.close(); 17 fos.close(); 18 System.out.println("Serialization done!"); 19 } catch (FileNotFoundException e) { 20 e.printStackTrace(); 21 } catch (IOException e2) { 22 e2.printStackTrace(); 23 } 24 25 } 26 }
控制台输出:
反序列化实例:
下面的例子通过使用FileInputStream和ObjectInputStream来进行反序列化,通过读取Person.ser文件,将字节流转成Person类的对象,
由于Person类的nationality参数声明为了transient,所以该参数不参与序列化化操作,导致反序列化该参数输出为null。
1 package com.led; 2 3 import com.led.javabase.Person; 4 5 import java.io.*; 6 7 /** 8 * @author Alan 9 * @Description: De-serialization Object 10 * @date 2018/4/22 12:36 11 */ 12 public class DeSerializtion { 13 public static void main(String[] args) { 14 Person o = null; 15 try { 16 FileInputStream fos = new FileInputStream("Person.ser"); 17 ObjectInputStream ois = new ObjectInputStream(fos); 18 o = (Person) ois.readObject(); 19 ois.close(); 20 fos.close(); 21 } catch (FileNotFoundException e) { 22 e.printStackTrace(); 23 return; 24 } catch (IOException e2){ 25 e2.printStackTrace(); 26 return; 27 } catch (ClassNotFoundException e) { 28 e.printStackTrace(); 29 return; 30 } 31 System.out.println("student's name: " + o.getName()); 32 System.out.println("student's age: " + o.getAge()); 33 System.out.println("student's gender: " + o.getGender()); 34 System.out.println("student's nationality: " + o.getNationality()); 35 36 } 37 }
控制台输出:
本文来自博客园,作者:Javanumberone,转载请注明原文链接:https://www.cnblogs.com/stm32stm32/p/8906161.html
posted on 2018-04-22 13:00 Javanumberone 阅读(259) 评论(0) 编辑 收藏 举报