序列化Serializable接口
一、序列化
1.什么是序列化?
序列化就是将对象的状态存储到特定存储介质中的过程,也就是将对象状态转换为可保持或传输格式的过程。
在序列化过程中,会将对象的公有成员、私有成员(包括类名),转换为字节流,然后再把字节流写入数据流,存储到存储介质中,这里说的存储介质通常指文件。
序列化后的对象保存的是二进制状态,这样实现了平台无关性,通过反序列化得到对象,而无需担心数据因平台问题而显示异常。
2.使用序列化保存对象信息
序列化机制允许将实现序列化的Java对象转换为字节序列,这个过程需要借助I/O流来实现。
只有实现了java.io.Serializable接口的类的对象才能被序列化,Serializable表示可串行的,可序列化的(串行化)。
JDK中如String类、包装类和Date类等,都实现了Serializable接口。
二、反序列化
使用反序列化获取对象信息:
1.创建一个对象输出流(ObjectInputStream),它可以包装一个其它类型的输入流。
2.通过对象输入流的readObject()方法读取对象,该方法返回一个object类型的对象,如果程序知道该Java对象的类型,则可以将该对象强制转换成其真实的类型。
1 import java.io.FileNotFoundException;
2 import java.io.FileOutputStream;
3 import java.io.IOException;
4 import java.io.ObjectOutputStream;
5
6 import bdqn.filedemo.Student;
7
8 /**
9 * 用序列化保存对象信息:
10 * 1.创建一个对象输出流(ObjectOutputStream),它可以包装一个其它类型的输出流。
11 * 2.通过对象输出流的writeObject()方法写对象,也就是输出可序列化对象。
12 * @author Administrator
13 *
14 */
15 public class SerializableObj {
16 /**
17 * 使用序列化将学生对象保存到文件中,实现步骤如下:
18 * 1.创建学生类,实现Serializable接口
19 * 2.引入相关类
20 * 3.创建对象输出流
21 * 4.调用writeObject()方法将对象写入文件
22 * 5.关闭对象输出流
23 * @param args
24 */
25 public static void main(String[] args) {
26 ObjectOutputStream oos = null;
27 try {
28 //创建ObjectOutputStream输出流
29 oos = new ObjectOutputStream(new FileOutputStream("D:\\tengyicheng\\stu.txt"));
30 Student stu = new Student("安娜",28,"女");
31 //对象序列化,写入输出流
32 oos.writeObject(stu);
33 System.out.println("录入成功!");
34 } catch (FileNotFoundException e) {
35 e.printStackTrace();
36 } catch (IOException e) {
37 e.printStackTrace();
38 }
39 finally{
40 if (oos!=null) {
41 try {
42 oos.close();
43 } catch (IOException e) {
44 e.printStackTrace();
45 }
46 }
47 }
48
49 }
50
51 }
1 import java.io.FileInputStream;
2 import java.io.IOException;
3 import java.io.ObjectInputStream;
4
5 import bdqn.filedemo.Student;
6
7 /**
8 * 使用反序列化获取对象信息:
9 * 1.创建一个对象输出流(ObjectInputStream),它可以包装一个其它类型的输入流。
10 * 2.通过对象输入流的readObject()方法读取对象,该方法返回一个object类型的对象,如果程序知道该Java对象的类型,则可以将该对象强制转换成其真实的类型。
11 * @author Administrator
12 *
13 */
14 public class DeSerializableObj {
15
16 /**
17 * 使用反序列化读取文件中的学生对象:
18 * 1.引入相关类
19 * 2.创建对象输入流
20 * 3.调用readObject()方法读取对象
21 * 4.关闭对象输入流。
22 * @param args
23 */
24 public static void main(String[] args) {
25 ObjectInputStream ois = null;
26 try {
27 //创建ObjectInputStream输入流
28 ois = new ObjectInputStream(new FileInputStream("D:\\tengyicheng\\stu.txt"));
29 Student object = (Student)ois.readObject();
30 System.out.println("姓名:"+object.getName());
31 System.out.println("年龄:"+object.getAge());
32 System.out.println("性别:"+object.getSex());
33 } catch (Exception e) {
34 e.printStackTrace();
35 }
36 finally{
37 if (ois!=null) {
38 try {
39 ois.close();
40 } catch (IOException e) {
41 e.printStackTrace();
42 }
43 }
44 }
45
46 }
47
48 }
1 import java.io.FileNotFoundException;
2 import java.io.FileOutputStream;
3 import java.io.IOException;
4 import java.io.ObjectOutputStream;
5 import java.util.LinkedList;
6 import java.util.List;
7
8 import bdqn.filedemo.Student;
9
10 /**
11 * 序列化一组对象
12 * @author Administrator
13 *
14 */
15 public class SerializableList {
16
17 public static void main(String[] args) {
18 ObjectOutputStream oos = null;
19 //创建一组学生对象
20 Student stu1 = new Student("李小冉",28,"女");
21 Student stu2 = new Student("赵丽颖",28,"女");
22 Student stu3 = new Student("胡歌",32,"男");
23 Student stu4 = new Student("王凯",35,"男");
24 //创建一组列表保存
25 List<Student> list = new LinkedList<Student>();
26 list.add(stu1);
27 list.add(stu2);
28 list.add(stu3);
29 list.add(stu4);
30 //创建对象输出流
31 try {
32 oos = new ObjectOutputStream(new FileOutputStream("D:\\tengyicheng\\stu1.txt"));
33 //写入输出流
34 oos.writeObject(list);
35 } catch (FileNotFoundException e) {
36 e.printStackTrace();
37 } catch (IOException e) {
38 e.printStackTrace();
39 }
40 finally{
41 if (oos!=null) {
42 try {
43 oos.close();
44 } catch (IOException e) {
45 e.printStackTrace();
46 }
47 }
48 }
49
50 }
51
52 }
1 import java.io.FileInputStream;
2 import java.io.FileNotFoundException;
3 import java.io.IOException;
4 import java.io.ObjectInputStream;
5 import java.util.LinkedList;
6 import java.util.List;
7
8 import bdqn.filedemo.Student;
9
10 /**
11 * 反序列化读取一组对象
12 * @author Administrator
13 *
14 */
15 public class DeSerializableList {
16
17 public static void main(String[] args) {
18 ObjectInputStream ois = null;
19 try {
20 //创建对象输入流
21 ois = new ObjectInputStream(new FileInputStream("D:\\tengyicheng\\stu1.txt"));
22 //读取对象
23 @SuppressWarnings("unchecked")
24 List<Student> stus = (LinkedList<Student>)ois.readObject();
25 for (Student stu : stus) {
26 System.out.println("姓名:"+stu.getName());
27 System.out.println("年龄:"+stu.getAge());
28 System.out.println("性别:"+stu.getSex());
29 }
30 } catch (FileNotFoundException e) {
31 e.printStackTrace();
32 } catch (ClassNotFoundException e) {
33 e.printStackTrace();
34 } catch (IOException e) {
35 e.printStackTrace();
36 }
37 finally{
38 if (ois!=null) {
39 try {
40 ois.close();
41 } catch (IOException e) {
42 e.printStackTrace();
43 }
44 }
45 }
46
47 }
48
49 }


浙公网安备 33010602011771号