吉林市网站建设,网站建设维护有哪些内容,北京师范大学学风建设专题网站,优化网站排名如何一、理解Java序列化和反序列化 
Serialization(序列化)#xff1a;将java对象以一连串的字节保存在磁盘文件中的过程#xff0c;也可以说是保存java对象状态的过程。序列化可以将数据永久保存在磁盘上(通常保存在文件中)。 
deserialization(反序列化)#xff1a;将保存在磁…一、理解Java序列化和反序列化 
Serialization(序列化)将java对象以一连串的字节保存在磁盘文件中的过程也可以说是保存java对象状态的过程。序列化可以将数据永久保存在磁盘上(通常保存在文件中)。 
deserialization(反序列化)将保存在磁盘文件中的java字节码重新转换成java对象称为反序列化。 
二、序列化和反序列化的应用 
两个进程在远程通信时可以发送多种数据包括文本、图片、音频、视频等这些数据都是以二进制序列的形式在网络上传输。 
java是面向对象的开发方式一切都是java对象想要在网络中传输java对象可以使用序列化和反序列化去实现发送发需要将java对象转换为字节序列然后在网络上传送接收方收到字符序列后会通过反序列化将字节序列恢复成java对象。 
java序列化的优点 实现了数据的持久化通过序列化可以把数据持久地保存在硬盘上(磁盘文件)。 利用序列化实现远程通信在网络上传输字节序列。 
三、序列化和反序列化地实现 
1.JDK类库提供的序列化API: 
java.io.ObjectOutputStream 表示对象输出流其中writeObject(Object obj)方法可以将给定参数的obj对象进行序列化将转换的一连串的字节序列写到指定的目标输出流中。 java.io.ObjectInputStream 该类表示对象输入流该类下的readObject(Object obj)方法会从源输入流中读取字节序列并将它反序列化为一个java对象并返回。 序列化要求 
实现序列化的类对象必须实现了Serializable类或Externalizable类才能被序列化否则会抛出异常。 
实现java序列化和反序列化的三种方法 
现在要对student类进行序列化和反序列化遵循以下方法 
方法一若student类实现了serializable接口则可以通过objectOutputstream和objectinputstream默认的序列化和反序列化方式对非transient的实例变量进行序列化和反序列化。 
方法二若student类实现了serializable接口并且定义了writeObject(objectOutputStream out)和 
readObject(objectinputStream in)方法则可以直接调用student类的两种方法进行序列化和反序列化。 
方法三若student类实现了Externalizable接口则必须实现readExternal(Objectinput in)和writeExternal(Objectoutput out)方法进行序列化和反序列化。 
JDK类库中的序列化步骤 
第一步创建一个输出流对象它可以包装一个输出流对象如文件输出流。 
ObjectOutputStream out  new ObjectOutputStream(new fileOutputStream(E:\\JavaXuLiehua\\Student\\Student1.txt));第二步通过输出流对象的writeObject()方法写对象 
out.writeObject(hollo word);out.writeObject(happy)JDK中反序列化操作 
第一步创建文件输入流对象 ObjectInputStream in  new ObjectInputStream(new fileInputStream(E:\\JavaXuLiehua\\Student\\Student1.txt));第二步调用readObject()方法 String obj1  (String)in.readObject();String obj2  (String)in.readObject();为了保证正确读取数据对象输出流写入对象的顺序与对象输入流读取对象的顺序一致。 
Student类序列化和反序列化演示 
1.先创建一个继承了serializable类的student类 
import java.io.Serializable;            //导入io包下的序列化类//创建实现序列化接口的学生类
public class Student implements Serializable {//私有化成员变量private String name;private  char sex;private  int year;private  double gpa;public Student(){   //无参构造}public Student(String name,char sex,int year,double gpa){//参数给属性赋值this.name  name;this.sex  sex;this.year  year;this.gpa  gpa;}//重写set和getpublic String getName() {return name;}public void setName(String name) {this.name  name;}public char getSex() {return sex;}public void setSex(char sex) {this.sex  sex;}public int getYear() {return year;}public void setYear(int year) {this.year  year;}public double getGpa() {return gpa;}public void setGpa(double gpa) {this.gpa  gpa;}
}把Student类的对象序列化到txt文件E:\JavaXuLiehua\Student\Student1.txt中并对文件进行反序列化 
import java.io.*;
import java.io.Externalizable;
/*
把student类对象序列化到文件E:\\JavaXuLiehua\\Student\\Student1.txt*/
public class UserStudent {public static void main(String[] args) throws IOException {Student st  new Student(Tom,M,20,3.6);         //实例化student类//判断Student1.txt是否创建成功File file  new File(E:\\JavaXuLiehua\\Student\\Student1.txt);if(file.exists()) {System.out.println(文件存在);}else{//否则创建新文件file.createNewFile();}try {//Student对象序列化过程FileOutputStream fos  new FileOutputStream(file);ObjectOutputStream oos  new ObjectOutputStream(fos);//调用 ObjectOutputStream 中的 writeObject() 方法 写对象oos.writeObject(st);oos.flush();        //flush方法刷新缓冲区写字符时会用因为字符会先进入缓冲区将内存中的数据立刻写出fos.close();oos.close();//Student对象反序列化过程FileInputStream fis  new FileInputStream(file);//创建对象输入流ObjectInputStream ois  new ObjectInputStream(fis);//读取对象Student st1  (Student) ois.readObject();           //会抛出异常类找不到异常System.out.println(name    st1.getName());System.out.println(sex    st1.getSex());System.out.println(year    st1.getYear());System.out.println(gpa    st1.getGpa());ois.close();fis.close();}catch (ClassNotFoundException e){e.printStackTrace();}}
}查看txt文件结果如下 
 sr JavaxulieHua.Studentd9Q藿Hf D gpaC sexI yearL namet Ljava/lang/String;xp 烫烫掏 M   t Tom可以看出其中的内容是不容易阅读的只能通过反序列化读取。 
四、transient关键字 
transient关键字表示有理的被修饰的数据不能进行序列化 
这里不做详细介绍修改情况如下 
private transient char sex;         //被transient关键字修饰不参与序列化运行结果如下 
文件存在
name  Tom
sex   
year  20
gpa  3.6此时可以看见被transient关键字修饰的变量sex并没有被序列化返回了空值。 
五、Externalizable接口实现序列化与反序列化 
Externalizable接口继承Serializable接口实现Externalizable接口需要实现readExternal()方法和writeExternal()方法这两个方法是抽象方法对应的是serializable接口的readObject()方法和writeObject()方法可以理解为把serializable的两个方法抽象出来。Externalizable没有serializable的限制static和transient关键字修饰的属性也能进行序列化。 
具体代码实现如下 
复制对象student命名为student1在里面重写writeExternal()方法和readExternal()方法如下 Override//对抽象方法进行重写public void writeExternal(ObjectOutput out) throws IOException{out.writeObject(name);out.writeObject(sex);out.writeObject(year);out.writeObject(gpa);}Overridepublic void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {name  (String) in.readObject();sex  (char) in.readObject();year  (int) in.readObject();gpa  (double) in.readObject();}相应的测试方法里面调用这两种方法的时候直接调用writeObject()方法和readObject()方法即可重写的writeExternal()和readExternal()方法会自动执行。 
FileOutputStream fos1  new FileOutputStream(file1);ObjectOutputStream oos1  new ObjectOutputStream(fos1);//调用 ObjectOutputStream 中的 writeObject() 方法 写对象oos1.writeObject(st);       //会自动执行重写的writeExternal()方法FileInputStream fis1  new FileInputStream(file1);//创建对象输入流ObjectInputStream ois1  new ObjectInputStream(fis1);//读取对象//会自动执行readExternal()方法Student1 st1  (Student1) ois1.readObject();           //会抛出异常类找不到异常虽然student1类里的sex属性被static或transient修饰但依旧被序列化结果如下 
文件存在
name  Tom
sex  M
year  20
gpa  3.6 文章转载自: http://www.morning.wwkdh.cn.gov.cn.wwkdh.cn http://www.morning.dhqzc.cn.gov.cn.dhqzc.cn http://www.morning.tnjz.cn.gov.cn.tnjz.cn http://www.morning.bhwz.cn.gov.cn.bhwz.cn http://www.morning.fwllb.cn.gov.cn.fwllb.cn http://www.morning.kcbml.cn.gov.cn.kcbml.cn http://www.morning.hbfqm.cn.gov.cn.hbfqm.cn http://www.morning.kgmkl.cn.gov.cn.kgmkl.cn http://www.morning.nxwk.cn.gov.cn.nxwk.cn http://www.morning.qhydkj.com.gov.cn.qhydkj.com http://www.morning.wxckm.cn.gov.cn.wxckm.cn http://www.morning.fbzyc.cn.gov.cn.fbzyc.cn http://www.morning.ykshx.cn.gov.cn.ykshx.cn http://www.morning.drbd.cn.gov.cn.drbd.cn http://www.morning.wpjst.cn.gov.cn.wpjst.cn http://www.morning.xlclj.cn.gov.cn.xlclj.cn http://www.morning.nclbk.cn.gov.cn.nclbk.cn http://www.morning.thntp.cn.gov.cn.thntp.cn http://www.morning.mhrzd.cn.gov.cn.mhrzd.cn http://www.morning.qxlxs.cn.gov.cn.qxlxs.cn http://www.morning.gfprf.cn.gov.cn.gfprf.cn http://www.morning.jlxld.cn.gov.cn.jlxld.cn http://www.morning.rkmsm.cn.gov.cn.rkmsm.cn http://www.morning.tqygx.cn.gov.cn.tqygx.cn http://www.morning.ygpdm.cn.gov.cn.ygpdm.cn http://www.morning.qfqld.cn.gov.cn.qfqld.cn http://www.morning.ebpz.cn.gov.cn.ebpz.cn http://www.morning.qxnns.cn.gov.cn.qxnns.cn http://www.morning.kghhl.cn.gov.cn.kghhl.cn http://www.morning.qtxwb.cn.gov.cn.qtxwb.cn http://www.morning.wncb.cn.gov.cn.wncb.cn http://www.morning.ksgjy.cn.gov.cn.ksgjy.cn http://www.morning.jyyw.cn.gov.cn.jyyw.cn http://www.morning.xyyplp.cn.gov.cn.xyyplp.cn http://www.morning.bkslb.cn.gov.cn.bkslb.cn http://www.morning.rcmwl.cn.gov.cn.rcmwl.cn http://www.morning.dzgyr.cn.gov.cn.dzgyr.cn http://www.morning.tmcmj.cn.gov.cn.tmcmj.cn http://www.morning.gl-group.cn.gov.cn.gl-group.cn http://www.morning.dwwlg.cn.gov.cn.dwwlg.cn http://www.morning.cthkh.cn.gov.cn.cthkh.cn http://www.morning.hgtr.cn.gov.cn.hgtr.cn http://www.morning.qnbzs.cn.gov.cn.qnbzs.cn http://www.morning.mdjtk.cn.gov.cn.mdjtk.cn http://www.morning.gqtxz.cn.gov.cn.gqtxz.cn http://www.morning.gmdtk.cn.gov.cn.gmdtk.cn http://www.morning.rqqlp.cn.gov.cn.rqqlp.cn http://www.morning.nqbs.cn.gov.cn.nqbs.cn http://www.morning.nkyc.cn.gov.cn.nkyc.cn http://www.morning.bqnhh.cn.gov.cn.bqnhh.cn http://www.morning.qtsks.cn.gov.cn.qtsks.cn http://www.morning.knrgb.cn.gov.cn.knrgb.cn http://www.morning.bkxnp.cn.gov.cn.bkxnp.cn http://www.morning.gzgwn.cn.gov.cn.gzgwn.cn http://www.morning.lwygd.cn.gov.cn.lwygd.cn http://www.morning.fykqh.cn.gov.cn.fykqh.cn http://www.morning.okiner.com.gov.cn.okiner.com http://www.morning.hqrkq.cn.gov.cn.hqrkq.cn http://www.morning.zbhfs.cn.gov.cn.zbhfs.cn http://www.morning.qkqjz.cn.gov.cn.qkqjz.cn http://www.morning.qphgp.cn.gov.cn.qphgp.cn http://www.morning.lwxsy.cn.gov.cn.lwxsy.cn http://www.morning.zrpbf.cn.gov.cn.zrpbf.cn http://www.morning.mkxxk.cn.gov.cn.mkxxk.cn http://www.morning.nbqwr.cn.gov.cn.nbqwr.cn http://www.morning.xxwhz.cn.gov.cn.xxwhz.cn http://www.morning.jzdfc.cn.gov.cn.jzdfc.cn http://www.morning.pmghz.cn.gov.cn.pmghz.cn http://www.morning.fbbmg.cn.gov.cn.fbbmg.cn http://www.morning.qbnfc.cn.gov.cn.qbnfc.cn http://www.morning.yxwcj.cn.gov.cn.yxwcj.cn http://www.morning.smyxl.cn.gov.cn.smyxl.cn http://www.morning.qjzgj.cn.gov.cn.qjzgj.cn http://www.morning.msxhb.cn.gov.cn.msxhb.cn http://www.morning.lhxdq.cn.gov.cn.lhxdq.cn http://www.morning.zzqgc.cn.gov.cn.zzqgc.cn http://www.morning.nypgb.cn.gov.cn.nypgb.cn http://www.morning.jrslj.cn.gov.cn.jrslj.cn http://www.morning.mnslh.cn.gov.cn.mnslh.cn http://www.morning.thrtt.cn.gov.cn.thrtt.cn