好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

详解Java 序列化与反序列化(Serialization)

一、什么是?为什么需要?

序列化(serialization)是将对象的状态信息转化为可以存储或者传输的形式的过程,反序列化则为其逆过程。

内存的易失性;传输需要;一些应用场景中需要将对象持久化下来,以便在需要的时候进行读取。

二、jdk提供的api

java.io.objectoutputstream类的 writeobject(object obj)方法

java.io.objectinputstream类的readobject()方法

对于serializable,如果没有重写 writeobject和readobject,则调用默认的方法

externalizable继承了serializable,多了2个方法:writeexternal和readexternal,用来控制需要序列化哪些字段

三、实现方法

假定一个person类,实现了serializable或externalizable接口

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

import java.io.serializable;

 

/**

  * @author: pf_xu

  * @date: 2019/3/5 12:37

  * @version 1.0

  */

public class person implements serializable {

 

  private int age;

  private string name;

 

  public person( int age, string name) {

  this .age = age;

  this .name = name;

  }

 

  public void setage( int age) {

  this .age = age;

  }

 

  public void setname(string name) {

  this .name = name;

  }

 

  public int getage() {

  return age;

  }

 

  public string getname() {

  return name;

  }

 

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

import java.io.externalizable;

import java.io.ioexception;

import java.io.objectinput;

import java.io.objectoutput;

 

/**

  * @author: pf_xu

  * @date: 2019/3/5 13:01

  * @version 1.0

  */

public class specialperson implements externalizable {

 

  private int age;

  private string name;

 

  public specialperson(){}

 

  public specialperson( int age, string name) {

  this .age = age;

  this .name = name;

  }

 

  public void setage( int age) {

  this .age = age;

  }

 

  public void setname(string name) {

  this .name = name;

  }

 

  public int getage() {

  return age;

  }

 

  public string getname() {

  return name;

  }

 

  @override

  public void writeexternal(objectoutput out) throws ioexception {

  out.writeobject(age);

  out.writeobject(name);

  }

 

  @override

  public void readexternal(objectinput in) throws ioexception, classnotfoundexception {

  this .age = (integer) in.readobject();

  this .name = (string)in.readobject();

  }

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

import java.io.*;

 

/**

  * @author: pf_xu

  * @date: 2019/3/5 12:40

  * @version 1.0

  */

public class serializabledemo {

  public static void main(string[] args) throws ioexception, classnotfoundexception {

 

  person person = new person( 10 , "simon" );

  objectoutputstream oos1 = new objectoutputstream( new fileoutputstream( "object1.out" ));

  oos1.writeobject(person);

  objectinputstream ois1= new objectinputstream( new fileinputstream( "object1.out" ));

  person re_person = (person) ois1.readobject();

  system.out.println(re_person.getname()+ "---" +re_person.getage());

 

  specialperson specialperson = new specialperson( 30 , "daniel" );

  objectoutputstream oos2 = new objectoutputstream( new fileoutputstream( "object2.out" ));

  oos2.writeobject(specialperson);

  objectinputstream ois2= new objectinputstream( new fileinputstream( "object2.out" ));

  specialperson re_specialperson = (specialperson)ois2.readobject();

  system.out.println(re_specialperson.getname()+ "---" +re_specialperson.getage());

 

  }

}

   四、一些细节

1.序列化id

serialversionuid  如果两个类的id不同,则不能互相序列与反序列(可应用与版本控制,不同版本的类相互兼容或者不兼容)

2.安全性

由于其标准化导致其有泄露的风险(二进制明文,可采用加密的方法)

以上所述是小编给大家介绍的java序列化和反序列化详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

原文链接:https://HdhCmsTestcnblogs测试数据/xupengfei/p/10476368.html

查看更多关于详解Java 序列化与反序列化(Serialization)的详细内容...

  阅读:18次