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]

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

相关文章

  • Day14基础不牢地动山摇-Java基础

    Day14基础不牢地动山摇-Java基础

    这篇文章主要给大家介绍了关于Java中方法使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-08-08
  • java子类调用父类的方法中包含子类重写的实例方法

    java子类调用父类的方法中包含子类重写的实例方法

    在本篇文章里小编给大家整理了关于java子类调用父类的方法中包含子类重写的实例方法以及相关知识点,需要的朋友们可以学习下。
    2019-09-09
  • Windows使用多个JDK的方法详解

    Windows使用多个JDK的方法详解

    本文介绍了如何在Windows系统中同时使用多个JDK版本(JDK8和JDK21),并详细描述了修改环境变量和Path变量的步骤,以实现JDK版本的切换
    2024-12-12
  • Java压缩之LZW算法字典压缩与解压讲解

    Java压缩之LZW算法字典压缩与解压讲解

    今天小编就为大家分享一篇关于Java压缩之LZW算法字典压缩与解压讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-02-02
  • jasypt dubbo配置密文存放使用详解

    jasypt dubbo配置密文存放使用详解

    这篇文章主要介绍了jasypt dubbo配置密文存放使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • Java开发环境配置JDK超详细整理(适合新手入门)

    Java开发环境配置JDK超详细整理(适合新手入门)

    这篇文章主要给大家介绍了关于Java开发环境配置JDK超详细整理的相关资料,非常适合新手入门,JDK是Java语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序,需要的朋友可以参考下
    2023-11-11
  • 剑指Offer之Java算法习题精讲数组与二叉树

    剑指Offer之Java算法习题精讲数组与二叉树

    跟着思路走,之后从简单题入手,反复去看,做过之后可能会忘记,之后再做一次,记不住就反复做,反复寻求思路和规律,慢慢积累就会发现质的变化
    2022-03-03
  • myEclipse配置jdk1.7教程

    myEclipse配置jdk1.7教程

    这篇文章主要为大家详细介绍了myEclipse配置jdk1.7教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • Spring Security单项目权限设计过程解析

    Spring Security单项目权限设计过程解析

    这篇文章主要介绍了Spring Security单项目权限设计过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • Mybatis中的自定义映射resultMap

    Mybatis中的自定义映射resultMap

    在MyBatis中,自定义映射resultMap可以让你精确控制如何将数据库返回的结果集映射到Java对象上,本文给介绍了Mybatis之自定义映射resultMap,需要的朋友可以参考下
    2024-03-03

最新评论