版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、对象序列化,目标,描述对象序列化的目标 编程实现对象序列化 编程实现序列化的对象在网络上的传输 掌握Serializable和Externalizable接口 使用transient关键字保护数据,概述,如何做到程序终止后职员A的信息以对象的形式继续存在?,如何将职员A的信息作为一个对象在网络上传输?,对象序列化,如何序列化?,实现下面两种接口之一 Serializable Externalizable,public class Person implement Serializable private String name; private int age; public Person (
2、String name,int age) =name; this.age=age; ,将对象保存到磁盘,java.io.ObjectOutputStream类 writeObject()方法,public class WritePerson public static void main(String args) try ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(person.txt); Person p=new Person(mary,30); oos.writeObject(p);
3、 oos.close(); catch(IOException e) e.printStackTrace(); ,从磁盘读出保存的对象,java.io.ObjectInputStream类 readObject()方法,public class ReadPerson public static void main(String args) Person p=null; try ObjectInputStream ois=new ObjectInputStream(new FileInputStream(person.txt); p=(Person )ois.readObject(); ois.
4、close(); catch(IOException e) e.printStackTrace(); ,在网络中传递对象,建立一个Socket连接,在这个连接之间传递系列化后的对象 示例,我不想保存我的密码,怎么办?,定制对象序列化,定制对象序列化,三个办法,使用transient关键字,实现Externalizable接口,定制对象系列化,在可序列化的类中实现方法writeObject(ObjectOutputStream out) readObject(ObjectInputStream in),示例,public class SerializableEx implements Seria
5、lizable private String name; private int age; private String sex; private String pwd; private String nationality; private void writeObject(ObjectOutputStream out) throws IOException String userpwd = encrypt(pwd); out.writeObject(name); out.writeObject(age); out.writeObject(sex); out.writeObject(user
6、pwd); out.writeObject(nationality); ,处理密码,示例,private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException = (String) in.readObject(); this.age = in.readInt(); this.sex = (String) in.readObject(); this.pwd = (String) in.readObject(); this.nationality = (String) i
7、n.readObject(); private String encrypt(String in) StringBuffer sb = new StringBuffer(in); sb.reverse(); return sb.toString(); ,transient关键字,通过在属性前面加上transient关键字,限制属性写入到文件或网络中 还可以在未实现系列化接口的引用类型属性前面加上transient关键字,避免对此类属性进行递归系列化时出现java.io.NotSerializableException异常,实现Externalizable接口,可以让需要系列化的类实现Seria
8、lizable接口的子接口Externalizable Externalizable接口表示实现该接口的类在序列化中由该类本身来控制信息的写出和读入。 Externalizable接口定义: package java.io;public interface Externalizable extends Serializable public void writeExternal(ObjectOutput out) throws IOException; public void readExternal(ObjectInput in) throws IOException, java.lang.
9、ClassNotFoundException;,实现Externalizable接口(续),步骤: 实现Externalizable接口 实现readExternal()/writeExternal()方法 需要一个不带参数的构造器(是否需要显式定义根据类是否有定义构造器而定),示例,public class ExternExample implements Externalizable String name; String credictcard; int age; double salary; public ExternExample() System.out.println(无参构造器
10、被调用); public ExternExample(String name,String credictcard,int age,double salary) =name; this.age =age; this.credictcard =credictcard; this.salary =salary; public void writeExternal(ObjectOutput out)throws IOException out.writeObject(name); out.writeInt(age); out.writeDouble(salary); public void readExternal(ObjectInput in)throws IOException,ClassNotFoundException name=(String)in.readObject(); age=in.readInt(); salary=in.readDouble(); ,可被序列化的内容,什么被序列化 属性(包括基本数据类型、数组、对其它对象的引用) 类
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 八年级道德与法治重点难点文化自信判断题易错题训练卷学霸挑战版
- 个性化职业发展规划方案
- 职业发展规划未来展望与策略
- 华为AI功能使用指南
- 抗微生物药物耐药诊治指南更新总结2026
- 2026年10月自考00230合同法押题及答案
- 法律职业资格客观题高频考点(完整版)
- 法律职业资格客观题考前押题试卷(解析版)
- 关于2026年库存管理调整执行通知函6篇
- 考研模拟试题及答案
- 2026年肾内科医生三基三严培训试卷及答案
- 2026年哈尔滨市道里区六年级下学期期末英语试题及答案
- 2026湖北武汉市区属国有企业招聘笔试历年参考题库附带答案详解
- 护理人员的情绪管理与礼仪
- 2026上海博物馆公开招聘12名工作人员考试备考试题及答案解析
- 2026年高考化学终极冲刺:专题03 化学反应原理综合题(大题专练逐空突破)(全国适用)(原卷版及解析)
- GB/T 31458-2026医院安全防范要求
- 实验室触电培训
- 政协提案知识培训讲稿
- 《2025年宁夏社区工作者招聘考试高频考点试题解析》
- 人工智能赋能乡村治理的实践逻辑、现实困境与优化路径
评论
0/150
提交评论