Java 对象序列化

谁借莪1个温暖的怀抱¢ 2024-04-01 14:49 151阅读 0赞

目录

一、序列化

定义:

方法:

代码:

二、反序列化

定义:

方法:

代码:

三、自定义类序列化

步骤:

代码:


一、序列化

定义:

将内存中的Java对象保存到磁盘中或通过网络传输过去

方法:

使用过ObjectOutputStream类实现。

另外,ObjectOutputStream和ObjectInputStream不能序列化static和transient的成员变量

代码:

  1. //序列化过程
  2. @Test
  3. public void test_Objectoutputstream(){
  4. ObjectOutputStream oos= null;
  5. try {
  6. oos = new ObjectOutputStream(new FileOutputStream("D:\\Java develop\\Program\\superior_Java\\hello5.dat"));
  7. String name="Ingram";
  8. oos.writeObject(name);
  9. oos.flush();
  10. } catch (IOException e) {
  11. throw new RuntimeException(e);
  12. } finally {
  13. if(oos!=null) {
  14. try {
  15. oos.close();
  16. } catch (IOException e) {
  17. throw new RuntimeException(e);
  18. }
  19. }
  20. }
  21. }

二、反序列化

定义:

将对象从磁盘或者网络中读取到内存中(程序中)的过程

方法:

使用ObjectInputStream类实现

代码:

  1. //反序列化过程
  2. @Test
  3. public void test_ObjectInputStream() {
  4. ObjectInputStream ois= null;
  5. try {
  6. ois = new ObjectInputStream(new FileInputStream("D:\\Java develop\\Program\\superior_Java\\hello5.dat"));
  7. Object s=ois.readObject();
  8. String str=(String)s;
  9. System.out.println(str);
  10. } catch (IOException e) {
  11. throw new RuntimeException(e);
  12. } catch (ClassNotFoundException e) {
  13. throw new RuntimeException(e);
  14. } finally {
  15. if(ois != null)
  16. try {
  17. ois.close();
  18. } catch (IOException e) {
  19. throw new RuntimeException(e);
  20. }
  21. }
  22. }

运行结果:2edc867777ec4d4b94283a6f2b8a2a42.png

三、自定义类序列化

步骤:

要实现自定义类序列化:

1.需要去实现接口:Serializable

2.需要为当前类提供一个全局常量:SerializableUID

3.除了当前类Person需要实现Serializable接口之外,还必须保证内部所有属性也必须是可序列化的

代码:

先定义一个Person类

  1. package Network_programming;
  2. import java.io.Serializable;
  3. /*要实现自定义类可序列化:
  4. * 1.需要实现接口:Serializable
  5. * 2.需要当前类提供一个全局常量:SerializableUID
  6. * 3.除了当前类Person需要实现Serializable接口之外,还必须保证内部所有属性也必须是可序列化的
  7. * */
  8. public class Person implements Serializable {
  9. public static final long serializableUID=23424324234L;
  10. private String name;
  11. private int number;
  12. public Person(String name, int number) {
  13. this.name = name;
  14. this.number = number;
  15. }
  16. public Person() {
  17. }
  18. /**
  19. * 获取
  20. * @return name
  21. */
  22. public String getName() {
  23. return name;
  24. }
  25. /**
  26. * 设置
  27. * @param name
  28. */
  29. public void setName(String name) {
  30. this.name = name;
  31. }
  32. /**
  33. * 获取
  34. * @return number
  35. */
  36. public int getNumber() {
  37. return number;
  38. }
  39. /**
  40. * 设置
  41. * @param number
  42. */
  43. public void setNumber(int number) {
  44. this.number = number;
  45. }
  46. public String toString() {
  47. return "Person{name = " + name + ", number = " + number + "}";
  48. }
  49. }

序列化与反序列化实现:

  1. package Network_programming;
  2. import org.junit.Test;
  3. import java.io.*;
  4. public class demo2 {
  5. public static void main(String[] args) {
  6. test_Objectoutputstream();
  7. test_ObjectInputStream();
  8. }
  9. public static void test_Objectoutputstream(){
  10. ObjectOutputStream oos= null;
  11. try {
  12. oos = new ObjectOutputStream(new FileOutputStream("D:\\Java develop\\Program\\superior_Java\\hello6.dat"));
  13. String name="Ingram";
  14. //oos.flush();
  15. oos.writeObject(name);
  16. oos.writeObject(new Person("ingram",14));
  17. oos.flush();
  18. } catch (IOException e) {
  19. throw new RuntimeException(e);
  20. } finally {
  21. if(oos!=null) {
  22. try {
  23. oos.close();
  24. } catch (IOException e) {
  25. throw new RuntimeException(e);
  26. }
  27. }
  28. }
  29. }
  30. //反序列化过程
  31. public static void test_ObjectInputStream() {
  32. ObjectInputStream ois= null;
  33. try {
  34. ois = new ObjectInputStream(new FileInputStream("D:\\Java develop\\Program\\superior_Java\\hello5.dat"));
  35. Object s=ois.readObject();
  36. String str=(String)s;
  37. System.out.println(str);
  38. System.out.println((Person)ois.readObject());
  39. } catch (IOException e) {
  40. throw new RuntimeException(e);
  41. } catch (ClassNotFoundException e) {
  42. throw new RuntimeException(e);
  43. } finally {
  44. if(ois != null)
  45. try {
  46. ois.close();
  47. } catch (IOException e) {
  48. throw new RuntimeException(e);
  49. }
  50. }
  51. }
  52. }

结果:991e9bbe6e0a4625a6871307f4ec8781.png

发表评论

表情:
评论列表 (有 0 条评论,151人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java序列对象

    java序列化 Java 提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字节序列,该字节序列包括该对象的数据、有关对象的类型的信息和存储在对象中数据的类型

    相关 Java对象序列

    [Java对象序列化][Java]   当两个进程在进行远程通信时,彼此可以发送各种类型的数据。无论是何种类型的数据,都会以二进制序列的形式在网络上传送。发送方需要把这个

    相关 Java 对象序列

    引言 将 [ Java][Java] 对象序列化为二进制文件的 Java 序列化技术是 Java 系列技术中一个较为重要的技术点,在大部分情况下,开发人员只需要了解被序列

    相关 Java对象序列

    Java对象的序列化 Java平台允许我们在内存中创建可复用的Java对象,但一般情况下,只有当JVM处于运行时,这些对象才可能存在,即,这些对象的生命周期不会比JVM的

    相关 java对象序列

    概念: Java 提供了一种对象序列化的机制,一个对象可以被表示为一个字节序列,该字节序列包括该对象的数据、有关对象的类型的信息和存储在对象中数据的类型。将序列化对象写入文件