ObjectInputStream及其相关知识

一、对象的序列化和反序列化

1、对象的序列化和反序列化

/** * 将对象序列化到文件中 * * @param obj 对象 * @param file 文件 * @throws IOException IO异常 */public static void writeObjectToFile(Object obj, File file) throws IOException {    try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {        oos.writeObject(obj);    }}/** * 从文件中反序列化得到对象 * * @param file 文件 * @param   对象类型 * @return 反序列化得到的对象 * @throws IOException            IO异常 * @throws ClassNotFoundException 找不到类异常 */public static  T readObjectFromFile(File file) throws IOException, ClassNotFoundException {    try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {        return (T) ois.readObject();    }}

2、对象的序列化和反序列化例子

public class SerializeObject {    public static void main(String[] args) throws IOException, ClassNotFoundException {        File file = new File("object.txt");        Student studentSrc = new Student("Tom", 18);        writeObjectToFile(studentSrc, file);        Student studentDst = readObjectFromFile(file);        System.out.println("studentDst = " + studentDst);    }}class Student implements Serializable {    private String name;    private int age;    public Student(String name, int age) {        this.name = name;        this.age = age;    }    @Override    public String toString() {        return "Student{" +                "name='" + name + '\'' +                ", age=" + age +                '}';    }}

二、ObjectInputStream类的详解

1、ObjectInputStream类简介

public class ObjectInputStream extends InputStream implements ObjectInput, ObjectStreamConstants {    protected ObjectInputStream() throws IOException, SecurityException {}    public ObjectInputStream(InputStream in) throws IOException {        verifySubclass();        bin = new BlockDataInputStream(in);        handles = new HandleTable(10);        vlist = new ValidationList();    }    private final BlockDataInput bin;    // more code omitted}

2、ObjectInputStream类的方法

/** *从流中读取一个Object对象 * * @throws IOException            IO异常 * @throws ClassNotFoundException 找不到类异常 */public final Object readObject() throws IOException, ClassNotFoundException { /* more code */ }/** * 从流中读取一个boolean类型的值 * * @throws IOException IO异常 */public boolean readBoolean() throws IOException { /* more code */ } 

三、ObjectOutputStream类及其相关类的详解

1、ObjectOutputStream类简介

public class ObjectOutputStream extends OutputStream implements ObjectOutput, ObjectStreamConstants {    /** 写入对象 */    public void writeObject(Object obj) throws IOException {...}    /** 获取包含此流中的所有对象曾经的引用 */    protected ObjectStreamClass getObjectStreamClass(Class cl) throws IOException, ClassNotFoundException {...}    /** 写入单个字节 */    public void write(int val) throws IOException {...}    /** 实现数据写入底层的操作系统 */    public void flush() throws IOException {...}    /** 关闭对象输出流 */    public void close() throws IOException {...}}final class BlockDataOutputStream {...}final class BlockDataInputStream {...}

2、ObjectOutputStream类的方法

/** * 将指定的基本类型写入流中。 */public final void writeInt(int val) throws IOException { /* more code */ }/** * 将单个double精度浮点数写入流中 */public final void writeDouble(double val) throws IOException { /* more code */ }/** * 写入一个UTF-8编码格式的字符串 */public final void writeUTF(String str) throws IOException { /* more code */ }

四、常用工具类的使用

1、IOUtils类

/** * 将InputStream转为byte[] * * @param inputStream 输入流 * @return byte[] * @throws IOException IO异常 */public static byte[] toByteArray(InputStream inputStream) throws IOException {    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();    byte[] buffer = new byte[1024 * 4];    int n;    while (-1 != (n = inputStream.read(buffer))) {        byteArrayOutputStream.write(buffer, 0, n);    }    return byteArrayOutputStream.toByteArray();}

2、ByteArrayUtils类

/** * 将序列化字节数组转为指定类型的对象 * * @param bytes 序列化字节数组 * @param clazz 指定类型 * @param    类型 * @return 对象 * @throws IOException            IO异常 * @throws ClassNotFoundException 找不到类异常 */public static  T deserialize(byte[] bytes, Class clazz) throws IOException, ClassNotFoundException {    try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);         ObjectInputStream ois = new ObjectInputStream(bis)) {        return (T) ois.readObject();    }}/** * 将对象序列化为字节数组 * * @param obj 对象 * @return 序列化字节数组 * @throws IOException IO异常 */public static byte[] serialize(Object obj) throws IOException {    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();         ObjectOutputStream oos = new ObjectOutputStream(bos)) {        oos.writeObject(obj);        return bos.toByteArray();    }}

五、总结

本文主要介绍了对象的序列化和反序列化知识及其相关类,同时也介绍了常用工具类的使用。通过学习,我们了解了对象的序列化和反序列化的原理和方法,了解了ObjectInputStream、ObjectOutputStream、BlockDataOutputStream、BlockDataInputStream等相关类的详细信息,以及IOUtils和ByteArrayUtils这两个常用的工具类的使用方法。希望对你的学习和工作有所帮助。

原创文章,作者:QXTZU,如若转载,请注明出处:https://www.506064.com/n/332014.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
QXTZUQXTZU
上一篇 2025-01-20 14:11
下一篇 2025-01-20 14:11

相关推荐

  • Fuzzer:什么是Fuzzer及其相关知识

    一、Fuzzer是什么 Fuzzer是一种用于发现软件漏洞的自动化安全测试工具。它能够以随机的方式输入数据、命令或文件,发现一些未被预期的错误或漏洞,这些错误或漏洞可以造成拒绝服务…

    编程 2025-04-23
  • tarxf命令-文件打包、压缩、解压缩工具的使用方法及相关知识

    一、泰安日系发型 点击进入泰安日系发型店铺评价页面,发现该店是泰安市区较受欢迎的男士理发店之一,提供了烫发、染发、剪发、理发等服务。其关注度,与tarxf命令有什么联系?实际上,t…

    编程 2025-04-12
  • BigDecimal初始化为0的相关知识

    一、BigDecimal-概述 BigDecimal是Java中的一个类,用于表示高精度的十进制数,提供高精度计算能力。它可以表示任意长度和精度的浮点数值,是一种更为精确的表示和计…

    编程 2025-02-24
  • Linux中修改系统时间及相关知识详解

    一、系统时间的意义 系统时间是指在Linux系统中记录的时间,用于文件的创建、修改、访问时间,日志记录等等。在一个社交媒体网站,每一个新发的帖子或图片,都会记录它的创建时间,方便其…

    编程 2025-02-05
  • Qt中延时相关知识详解

    一、延时的原理 在Qt中,延时指的是程序暂停一段时间后再继续执行。了解延时的原理可以更方便地使用延时相关函数。 Qt中延时的实现涉及到操作系统,因为延时需要用到操作系统提供的定时器…

    编程 2025-01-21
  • 深入了解Oracle时间相关知识

    一、Oracle时间介绍 Oracle数据库是一种关系型数据库管理系统,时间在其中扮演了至关重要的角色。Oracle数据库的时间记录使用内部时间格式存储,以便在表达式中进行比较和计…

    编程 2025-01-21
  • 深入解析quantile函数的相关知识

    一、quantile函数 量化函数(quantile function)是一个统计学上的概念,允许我们从一个已知的概率分布中计算出一个给定的概率值的对应值。 R 语言中提供了很多不…

    编程 2025-01-20
  • 浅谈pcilan的相关知识

    一、pci蓝牙 PCI蓝牙是一种无线通信技术,其通过计算机的PCI接口实现了高速连接蓝牙设备的功能。 对于使用PC机的用户们来说,可以通过PC机内置的PCI蓝牙接口来无线连接各种蓝…

    编程 2025-01-14
  • c语言文件常见,c语言文件相关知识

    本文目录一览: 1、c语言中有哪些常用的头文件? 2、在C语言中:常见的文件打开方式及含义 3、C语言的文件类型有哪些? 4、c语言中文件类型有几种? c语言中有哪些常用的头文件?…

    编程 2024-12-28
  • mysqladddate函数的使用及其相关知识

    一、 mysqladddate函数的概述 MySQL是一个开源、关系型数据库管理系统,是LAMP(Linux、Apache、MySQL、PHP/Perl/Python)或LEMP(…

    编程 2024-12-28

发表回复

登录后才能评论