Java原生序列化和反序列化代码实例

 更新时间:2020年02月12日 13:44:51   作者:Nick Huang  
这篇文章主要介绍了Java原生序列化和反序列化代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了Java原生序列化和反序列化代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

写一个Java原生的序列化和反序列化的DEMO。

需序列化的类:

package com.nicchagil.nativeserialize;

import java.io.Serializable;

public class User implements Serializable {

  private static final long serialVersionUID = 1L;

  private Integer id;
  private String userName;
  
  public User(Integer id, String userName) {
    super();
    this.id = id;
    this.userName = userName;
  }

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public static long getSerialversionuid() {
    return serialVersionUID;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    result = prime * result
        + ((userName == null) ? 0 : userName.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    User other = (User) obj;
    if (id == null) {
      if (other.id != null)
        return false;
    } else if (!id.equals(other.id))
      return false;
    if (userName == null) {
      if (other.userName != null)
        return false;
    } else if (!userName.equals(other.userName))
      return false;
    return true;
  }

  @Override
  public String toString() {
    return "User [id=" + id + ", userName=" + userName + "]";
  }

}

工具类:

package com.nicchagil.nativeserialize;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class NativeSerializeTools {

  /**
   * 序列化
   * @param filePath 序列化的路径
   * @param s 序列化的对象
   */
  public static void write(String filePath, Serializable s) throws FileNotFoundException, IOException {
    if (filePath == null || filePath.length() == 0) {
      throw new RuntimeException("请传入序列化路径");
    }
    
    if (s == null) {
      throw new RuntimeException("请传入序列化对象");
    }
    
    File f = new File(filePath);
    
    ObjectOutputStream oos = null;
    FileOutputStream fos = null;
    try {
      fos = new FileOutputStream(f);
      oos = new ObjectOutputStream(fos);
      oos.writeObject(s);
      System.out.println("finish.");
    } finally {
      if (oos != null) {
        oos.close();
      }
      if (fos != null) {
        fos.close();
      }
      System.out.println("close the resource.");
    }
  }
  
  /**
   * 反序列化
   * @param filePath 反序列化的路径
   * @return 反序列化的对象
   */
  public static Object read(String filePath) throws ClassNotFoundException, FileNotFoundException, IOException {
    if (filePath == null || filePath.length() == 0) {
      throw new RuntimeException("请传入反序列化路径");
    }
    
    File f = new File(filePath);
    
    ObjectInputStream ois = null;
    FileInputStream fis = null;
    Object o = null;
    try {
      fis = new FileInputStream(f);
      ois = new ObjectInputStream(fis);
      o = ois.readObject();
      System.out.println("finish.");
    } finally {
      if (ois != null) {
        ois.close();
      }
      if (fis != null) {
        fis.close();
      }
      System.out.println("close the resource.");
    }
    
    return o;
  }

}

测试类:

package com.nicchagil.nativeserialize;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.junit.Assert;
import org.junit.Test;

public class HowToUse {
  
  private User user = new User(100, "Nick Huang");
  private String filePath = "d:/user.txt";
  
  @Test
  public void c1() throws FileNotFoundException, IOException {
    NativeSerializeTools.write(filePath, user);
  }
  
  @Test
  public void c2() throws FileNotFoundException, IOException, ClassNotFoundException {
    Object o = NativeSerializeTools.read(filePath);
    
    System.out.println(o);
    Assert.assertTrue(user.equals(o));
  }

}

日志:

finish.
close the resource.
finish.
close the resource.
User [id=100, userName=Nick Huang]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java手动创建线程池代码实例

    Java手动创建线程池代码实例

    这篇文章主要介绍了Java手动创建线程池代码实例,FixedThreadPool或者SingleThreadPool,允许的请求队列长度为Integer.MAX_VALUE,可能会堆积大量的请求,从而导致OOM,需要的朋友可以参考下
    2023-12-12
  • Java中Iterator迭代器的简单理解

    Java中Iterator迭代器的简单理解

    这篇文章主要介绍了Java中Iterator迭代器的简单理解,Iterator接口也是Java集合中的一员,但它与Collection、Map接口有所不同,Iterator主要用于迭代访问Collection中的元素,因此Iterator对象也被称为迭代器,需要的朋友可以参考下
    2024-01-01
  • SpringCloud入门实验环境搭建

    SpringCloud入门实验环境搭建

    这篇文章主要介绍了SpringCloud入门实验环境搭建的相关资料,帮助大家更好的理解和学习使用SpringCloud,感兴趣的朋友可以了解下
    2021-04-04
  • SpringCloud微服务之Hystrix组件实现服务熔断的方法

    SpringCloud微服务之Hystrix组件实现服务熔断的方法

    微服务架构特点就是多服务,多数据源,支撑系统应用。这样导致微服务之间存在依赖关系。这篇文章主要介绍了SpringCloud微服务之Hystrix组件实现服务熔断的方法,需要的朋友可以参考下
    2019-08-08
  • Springboot @Value使用代码实例

    Springboot @Value使用代码实例

    这篇文章主要介绍了Springboot @Value使用代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • Spring全局懒加载的优劣及使用方法详解

    Spring全局懒加载的优劣及使用方法详解

    这篇文章主要介绍了Spring全局懒加载的优劣及使用方法详解,Spring 懒加载是一种延迟加载的机制,它允许在需要时才创建和初始化对象,而不是在应用程序启动时就立即加载所有对象,通过懒加载,可以提高应用程序的性能和资源利用率,需要的朋友可以参考下
    2023-10-10
  • springboot 集成支付宝支付的示例代码

    springboot 集成支付宝支付的示例代码

    这篇文章主要介绍了springboot 集成支付宝支付的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • JAVA String类中的一些常用方法示例详解

    JAVA String类中的一些常用方法示例详解

    在我们的工作中,常常要对一个字符串进行一些操作,这里提供一些常用的方法,常常需要这些方法进行组合处理字符串,这篇文章主要给大家介绍了关于JAVA String类中的一些常用方法,需要的朋友可以参考下
    2023-10-10
  • SpringBoot整合EasyExcel进行大数据处理的方法详解

    SpringBoot整合EasyExcel进行大数据处理的方法详解

    EasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目。在尽可能节约内存的情况下支持读写百M的Excel。本文将在SpringBoot中整合EasyExcel进行大数据处理,感兴趣的可以了解一下
    2022-05-05
  • Spring学习笔记之bean生命周期

    Spring学习笔记之bean生命周期

    Spring Bean是Spring应用中最最重要的部分了。下面这篇文章主要给大家介绍了关于Spring学习笔记之bean生命周期的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-12-12

最新评论