好得很程序员自学网

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

Java对象序列化操作详解

本文实例讲述了java 对象序列化 操作。分享给大家供大家参考,具体如下:

当两个进程在进行远程通信时,彼此可以发送各种类型的数据。无论是何种类型的数据,都会以 二进制序列 的形式在网络上传送。发送方需要把这个java对象转换为字节序列,才能在网络上传送;接收方则需要把字节序列再恢复为java对象。

只能将支持 java.io.serializable 接口的对象写入流中。每个 serializable 对象的类都被编码,编码内容包括类名和类签名、对象的字段值和数组值,以及从初始对象中引用的其他所有 对象的闭包 。

概念

序列化:把java对象转换为字节序列的过程。
反序列化:把字节序列恢复为java对象的过程。

用途

对象的序列化主要有两种用途:

1) 把对象的字节序列永久地保存到硬盘上,通常存放在一个文件中;
2) 在网络上传送对象的字节序列。

对象序列化

 

序列化api

java.io.objectoutputstream 代表对象输出流,它的 writeobject(object obj) 方法可对参数指定的obj对象进行序列化,把得到的字节序列写到一个目标输出流中。只有实现了serializable和externalizable接口的类的对象才能被序列化。

java.io.objectinputstream 代表对象输入流,它的 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

27

28

29

30

31

32

import java.io.*;

import java.util.date;

public class objectsaver {

   public static void main(string[] args) throws exception {

     /*其中的 d:\\objectfile.obj 表示存放序列化对象的文件*/

     //序列化对象

     objectoutputstream out = new objectoutputstream( new fileoutputstream( "d:\\objectfile.obj" ));

     customer customer = new customer( "王麻子" , 24 ); 

     out.writeobject( "你好!" );  //写入字面值常量

     out.writeobject( new date());  //写入匿名date对象

     out.writeobject(customer);  //写入customer对象

     out.close();

     //反序列化对象

     objectinputstream in = new objectinputstream( new fileinputstream( "d:\\objectfile.obj" ));

     system.out.println( "obj1 " + (string) in.readobject());  //读取字面值常量

     system.out.println( "obj2 " + (date) in.readobject());  //读取匿名date对象

     customer obj3 = (customer) in.readobject();  //读取customer对象

     system.out.println( "obj3 " + obj3);

     in.close();

   }

}

class customer implements serializable {

   private string name;

   private int age;

   public customer(string name, int age) {

     this .name = name;

     this .age = age;

   }

   public string tostring() {

     return "name=" + name + ", age=" + age;

   }

}

执行结果

说明

读取对象的顺序与写入时的顺序要一致。

对象的默认序列化机制写入的内容是:对象的类,类签名,以及非瞬态和非静态字段的值。

常见序列化操作

 

打印流

?

1

2

3

4

5

6

7

8

9

10

11

public class hello {

   public static void main(string[] args) throws exception {

    file file = new file( "e:" + file.separator + "myfile" + file.separator + "test" + file.separator + "123.txt" );

    outputstream outputstream = new fileoutputstream(file);

    printstream printstream = new printstream(outputstream);

    printstream.print( 123 );

    printstream.println( "hello" );

    printstream.println( 12.5 );

    printstream.close();

   }

}

键盘输入读取到程序中

?

1

2

3

4

5

6

7

8

9

10

public class hello {

   public static void main(string[] args) throws exception {

    inputstream in = system.in;

    byte [] data = new byte [ 100 ];

    system.out.println( "输入数据:" );

    int read = in.read(data);

    system.out.println(read);

    system.out.println( new string(data, 0 ,read));

   }

}

扫码流

?

1

2

3

4

5

6

7

8

9

10

11

public class hello {

   public static void main(string[] args) throws exception {

    scanner scanner = new scanner( new fileinputstream( new file( "e:" + file.separator + "myfile" + file.separator + "test" + file.separator + "123.txt" )));

    scanner.usedelimiter( "\n" );

    while (scanner.hasnext()){

      string next = scanner.next();

      system.out.println(next);

    }

    scanner.close();

   }

}

scanner.usedelimiter("\n"); 表示以回车(换行)为定界符,回车间为一段扫码的内容。

扫描键盘输入

?

1

scanner scanner = new scanner(system.in);

注意:使用while判断键盘输入,程序可能会无法结束

对象序列化

序列化操作类:objectoutputstream,写到文件中

?

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

public class hello {

   public static void main(string[] args) throws exception {

    a a = new a( "hello" , 123 );

    file file = new file( "e:" + file.separator + "myfile" + file.separator + "test" + file.separator + "a.ser" );

    outputstream outputstream = new fileoutputstream(file);

    objectoutputstream objectoutputstream = new objectoutputstream(outputstream);

    objectoutputstream.writeobject(a);

    objectoutputstream.close();

   }

}

class a implements serializable {

   private string title;

   private integer number;

   public a(string title, integer number) {

    this .title = title;

    this .number = number;

   }

   public string gettitle() {

    return title;

   }

   public void settitle(string title) {

    this .title = title;

   }

   public integer getnumber() {

    return number;

   }

   public void setnumber(integer number) {

    this .number = number;

   }

   @override

   public string tostring() {

    return "a{" +

       "title='" + title + '\ '' +

       ", number=" + number +

       '}' ;

   }

}

实体需要实现可序列化的接口 implements serializable ,表示一种能力

反序列化操作类:objectinputstream,读到程序里

?

1

2

3

4

5

6

7

8

9

public class hello {

   public static void main(string[] args) throws exception {

    file file = new file( "e:" + file.separator + "myfile" + file.separator + "test" + file.separator + "a.ser" );

    inputstream inputstream = new fileinputstream(file);

    objectinputstream objectinputstream = new objectinputstream(inputstream);

    a a = (a) objectinputstream.readobject();

    system.out.println(a);

   }

}

transient 关键字,实体的属性使用该关键子,进行序列化时该属性值将不会被保存,反序列化的结果为,该属性的值为该属性类型的默认值。

?

1

2

private string title;

private transient integer number;

希望本文所述对大家java程序设计有所帮助。

原文链接:https://blog.csdn.net/shuair/article/details/82013196

查看更多关于Java对象序列化操作详解的详细内容...

  阅读:56次