温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在Java中实现对象序列化操作

发布时间:2021-06-08 16:52:23 来源:亿速云 阅读:154 作者:Leah 栏目:编程语言

本篇文章给大家分享的是有关如何在Java中实现对象序列化操作,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

概念

序列化:把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;
  }
}

执行结果

如何在Java中实现对象序列化操作

说明

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

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

常见序列化操作

打印流

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中实现对象序列化操作,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI