Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed introduction of java serialization mechanism

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

本篇内容介绍了"java序列化机制详细介绍"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

序列化是指对象通过写出描述自己状态的数值来记录自己的过程,即将对象表示成一系列有序字节,Java提供了将对象写入流和从流中恢复对象的方法。对象能包含其它的对象,而其它的对象又可以包含另外的对象。Java序列化能够自动的处理嵌套的对象。对于一个对象的简单域,writeObject()直接将其值写入流中。

当遇到一个对象域时,writeObject()被再次调用,如果这个对象内嵌另一个对象,那么,writeObject()又被调用,直到对象能被直接写入流为止。程序员所需要做的是将对象传入ObjectOutputStream的writeObject()方法,剩下的将有系统自动完成。

要实现序列化的类必须实现的java.io.Serializable或java.io.Externalizable接口,否则将产生一个NotSerializableException。该接口内部并没有任何方法,它只是一个"tagging interface",仅仅"tags"它自己的对象是一个特殊的类型。

类通过实现 java.io.Serializable接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。可序列化类的所有子类型本身都是可序列化的。

序列化接口没有方法或字段,仅用于标识可序列化的语义。Java的"对象序列化"能让你将一个实现了Serializable接口的对象转换成一组byte,这样日后要用这个对象时候,你就能把这些byte数据恢复出来,并据此重新构建那个对象了。

代码如下

package stream;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;import javax.security.auth.login.AccountException;import org.junit.jupiter.api.Test;/* * 对象流 * 用与存储和读取基本数据类型数据 或 对象的处理刘。它的强大之处就是可以把java中的对象转换成流 * * */public class ObjectInputStreamTeat { /* * 序列化过程:将内存中的java对象保存到磁盘或通过网络传输出去 * 使用ObjectOutputStream * */ @Test public void testObjectOutputStream() { ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(new FileOutputStream("Object.dat")); oos.writeObject(new String("我是中国人")); oos.flush(); oos.writeObject(new Person("zsben",1,new Account(2000))); oos.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { if(oos!=null) oos.close(); } catch (Exception e) { e.printStackTrace(); } } } /* * 反序列化:用ObjectInputStream实现 * */ @Test public void testObjectInputStream() { ObjectInputStream ois = null; try { ois = new ObjectInputStream(new FileInputStream("Object.dat")); Object object = ois.readObject(); String string = (String)object; System.out.println(string); Object object2 = ois.readObject(); Person person = (Person)object2; System.out.println(person); } catch (Exception e) { e.printStackTrace(); } finally { try { if(ois!=null) ois.close(); } catch (Exception e) { e.printStackTrace(); } } }}/* * 1.Person类要实现可序列化,必须实现可序列化接口Serializable * 2.还需要添加一个最终类属性UID * 3.除了当前Person类需要实现Serializable,还要保证其内部所有属性都是可序列化的 * (默认情况下基本数据类型为可序列化) * 对象流不能序列化static 和transient修饰的成员 * */class Person implements Serializable{ public static final long serivalVersionUID = 5432146546351568416L; private String name; private static int age; private Account account; @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", account=" + account + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Person(String name, int age) { super(); this.name = name; this.age = age; } public Person(String name, int age, Account account) { super(); this.name = name; this.age = age; this.account = account; } public Person() { super(); } }class Account implements Serializable{ private double balance; static final public long serivalVersionUID = 54685237864535874L; @Override public String toString() { return "Account [balance=" + balance + "]"; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public Account(double balance) { super(); this.balance = balance; } public Account() { super(); }}"java序列化机制详细介绍"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report