首页 > 编程 > Java > 正文

Java对象序列化操作详解

2019-11-26 09:40:26
字体:
来源:转载
供稿:网友

本文实例讲述了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()方法从一个源输入流中读取字节序列,再把它们反序列化为一个对象,并将其返回。

代码示例

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;  }}

执行结果

说明

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

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

常见序列化操作

打印流

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();  }}

键盘输入读取到程序中

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));  }}

扫码流

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");表示以回车(换行)为定界符,回车间为一段扫码的内容。

扫描键盘输入

Scanner scanner = new Scanner(System.in);

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

对象序列化

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

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,读到程序里

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关键字,实体的属性使用该关键子,进行序列化时该属性值将不会被保存,反序列化的结果为,该属性的值为该属性类型的默认值。

private String title;private transient Integer number;

更多java相关内容感兴趣的读者可查看本站专题:《Java面向对象程序设计入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表