序列化和反序列化——基础加练习
一:序列化和反序列化:
1.序列化是将Java编写的类存储到文件中,他是一些看不懂得东西,反序列化就是将文件里面的东西写到程序中来
2.序列化技术:
1 import java.io.FileOutputStream; 2 import java.io.ObjectOutputStream; 3 import java.io.Serializable; 4 5 public class Test { 6 /** 7 * 序列化技术,将类存储到文件中,以便永久保存 8 * @param args 9 */ 10 public static void main(String[] args) throws Exception { 11 //创建图书对象 12 Book book = new Book("金苹果","讲诉了农夫种植苹果的心酸过程"); 13 //创建序列化对象 14 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D://java01")); 15 oos.writeObject(book); 16 oos.close(); 17 } 18 //implements Serializable 用于标记该类将要序列化 19 static class Book implements Serializable { 20 String name; 21 String info; 22 public Book(){}; 23 24 public Book(String name, String info) { 25 this.name = name; 26 this.info = info; 27 } 28 29 @Override 30 public String toString() { 31 return "Book{" + 32 "name='" + name + '\'' + 33 ", info='" + info + '\'' + 34 '}'; 35 } 36 } 37 }
3.反序列化技术:
1 import java.io.FileInputStream; 2 import java.io.ObjectInputStream; 3 import java.io.Serializable; 4 5 public class Test { 6 /** 7 * 反序列化技术,文件中储存的类,写到程序中 8 * 9 * @param args 10 */ 11 public static void main(String[] args) throws Exception { 12 //创建反序列化对象 13 ObjectInputStream obs = new ObjectInputStream(new FileInputStream("D://java01")); 14 // Object o = obs.readObject(); 15 // System.out.println(o); 16 Book o = (Book)obs.readObject(); 17 System.out.println(o); 18 } 19 20 //implements Serializable 用于标记该类将要序列化 21 static class Book implements Serializable { 22 String name; 23 String info; 24 25 public Book() { 26 } 27 28 public Book(String name, String info) { 29 this.name = name; 30 this.info = info; 31 } 32 33 @Override 34 public String toString() { 35 return "Book{" + 36 "name='" + name + '\'' + 37 ", info='" + info + '\'' + 38 '}'; 39 } 40 } 41 }
(如果反序列化中的类中有其他的引用数据类型,那么那个引用数据类型也要实现标记接口)
4.序列化和反序列化是用来数据传输或者本地存储的
二:部分序列化和反序列化:
1.分类:
1)使用transient修饰,修饰后该属性不将被序列化和反序列化
1 //实体类中的属性 2 //这两个属性不会被序列化和反序列化 3 private transient String name; 4 private transient int age;
2)使用static 修饰,修饰后该属性不将被序列化和反序列化
1 1 //实体类中的属性 2 2 //这两个属性不会被序列化和反序列化 3 3 private static String name; 4 4 private static int age;
3)使用方法writeObject()和readObject(),在该方法类,是要序列化的对象属性
1 //该方法写在实体类,对实体类进行序列化和反序列化 2 3 //将属性序列化 4 private void writeObject(java.io.ObjectOutputStream out) throws IOException { 5 out.writeObject(name); 6 out.writeObject(age); 7 } 8 //将属性反序列化 9 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { 10 name = (String) in.readObject(); 11 age = (int) in.readObject(); 12 }
4)Externalizable接口实现序列化和反序列化
4.1)Externalizable接口继承自Serilazable接口,实现该接口要重写readExternal()方法和writeExternal()方法
4.2)代码演示
1 import java.io.*; 2 3 public class Worker implements Externalizable { 4 5 private String name; 6 private int age; 7 8 public Worker(){}; 9 10 public Worker(String name, int age) { 11 this.name = name; 12 this.age = age; 13 } 14 15 public String getName() { 16 return name; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 public int getAge() { 24 return age; 25 } 26 27 public void setAge(int age) { 28 this.age = age; 29 } 30 31 @Override 32 public String toString() { 33 return "Worker{" + 34 "name='" + name + '\'' + 35 ", age=" + age + 36 '}'; 37 } 38 39 40 /** 41 * 实现序列化 42 * @param out 43 * @throws IOException 44 */ 45 @Override 46 public void writeExternal(ObjectOutput out) throws IOException { 47 out.writeObject(name); 48 out.writeObject(age); 49 } 50 51 /** 52 * 实现反序列化 53 * @param in 54 * @throws IOException 55 * @throws ClassNotFoundException 56 */ 57 @Override 58 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 59 name = (String)in.readObject(); 60 age = (int)in.readObject(); 61 } 62 }