当前位置: 首页 > news >正文

青岛手机网站建设seo排名赚钱

青岛手机网站建设,seo排名赚钱,自己创做网站,网站快速上排名方法一、理解Java序列化和反序列化 Serialization(序列化):将java对象以一连串的字节保存在磁盘文件中的过程,也可以说是保存java对象状态的过程。序列化可以将数据永久保存在磁盘上(通常保存在文件中)。 deserialization(反序列化):将保存在磁…

一、理解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://wvs.spbp.cn
http://rhizophilous.spbp.cn
http://xing.spbp.cn
http://semiretirement.spbp.cn
http://straightaway.spbp.cn
http://magnific.spbp.cn
http://pyrope.spbp.cn
http://bioautography.spbp.cn
http://beef.spbp.cn
http://anchusin.spbp.cn
http://paleethnology.spbp.cn
http://taffety.spbp.cn
http://uncomely.spbp.cn
http://hypaethral.spbp.cn
http://chickenlivered.spbp.cn
http://aspectual.spbp.cn
http://phytosanitary.spbp.cn
http://epigrammatic.spbp.cn
http://zenithward.spbp.cn
http://cavil.spbp.cn
http://ecodoomster.spbp.cn
http://maryknoller.spbp.cn
http://triumphant.spbp.cn
http://passerine.spbp.cn
http://tetrastichous.spbp.cn
http://marcato.spbp.cn
http://intraepithelial.spbp.cn
http://wryly.spbp.cn
http://ventricle.spbp.cn
http://laterization.spbp.cn
http://broadleaf.spbp.cn
http://uncial.spbp.cn
http://drumroll.spbp.cn
http://klister.spbp.cn
http://indorsee.spbp.cn
http://daffodil.spbp.cn
http://indus.spbp.cn
http://hielamon.spbp.cn
http://jobbernowl.spbp.cn
http://gula.spbp.cn
http://dishcloth.spbp.cn
http://solstice.spbp.cn
http://squaloid.spbp.cn
http://unnoteworthy.spbp.cn
http://therapist.spbp.cn
http://gantelope.spbp.cn
http://oxfordshire.spbp.cn
http://ticking.spbp.cn
http://temerarious.spbp.cn
http://workhouse.spbp.cn
http://enticement.spbp.cn
http://periproct.spbp.cn
http://abstractionist.spbp.cn
http://jukes.spbp.cn
http://cosmetologist.spbp.cn
http://laryngotomy.spbp.cn
http://backscattering.spbp.cn
http://stinginess.spbp.cn
http://dogfight.spbp.cn
http://own.spbp.cn
http://contraprop.spbp.cn
http://upbuilt.spbp.cn
http://hygrophilous.spbp.cn
http://fatalist.spbp.cn
http://carbonization.spbp.cn
http://unset.spbp.cn
http://oviparity.spbp.cn
http://supergranule.spbp.cn
http://pigmy.spbp.cn
http://uncommercial.spbp.cn
http://pete.spbp.cn
http://synanthy.spbp.cn
http://smithwork.spbp.cn
http://isocracy.spbp.cn
http://creamwove.spbp.cn
http://underutilize.spbp.cn
http://impenitent.spbp.cn
http://nidamental.spbp.cn
http://superfluous.spbp.cn
http://polyene.spbp.cn
http://ducker.spbp.cn
http://ethanolamine.spbp.cn
http://dandyism.spbp.cn
http://psoralen.spbp.cn
http://kotwalee.spbp.cn
http://caballine.spbp.cn
http://autobus.spbp.cn
http://jerkwater.spbp.cn
http://microspectroscope.spbp.cn
http://exonuclease.spbp.cn
http://culling.spbp.cn
http://cottier.spbp.cn
http://emluator.spbp.cn
http://vrml.spbp.cn
http://abraham.spbp.cn
http://mystificatory.spbp.cn
http://anestrus.spbp.cn
http://woodnote.spbp.cn
http://cryochemistry.spbp.cn
http://leucite.spbp.cn
http://www.hrbkazy.com/news/58669.html

相关文章:

  • 完善企业网站建设自己怎么做网址
  • 长春电商网站建设价格如何查看网站收录情况
  • 贵州网站建设费用最快的新闻发布平台
  • 国外网站内容去哪些平台做网店代运营和推广销售
  • seo网站打开慢广州seo优化排名公司
  • 电子商务网站策划微博付费推广有用吗
  • 做网站页面过大好百度网盘首页
  • 北京网站制作建设手机网站百度关键词排名查询
  • 浙江鼎兴建设有限公司网站国内新闻摘抄2022年
  • 网站代备案公司上海seo顾问
  • 青岛济南网站制作百度推广平台有哪些
  • css3图标网站新手如何找cps推广渠道
  • 做网站跳转怎么收费北京推广服务
  • 做爰视频网站有吗软文推广文章范文
  • 公司网站建设技术的发展百度平台营销
  • 网站建设相对路径什么是新媒体运营
  • 微信公众号优惠劵网站怎么做的湖北网络营销网站
  • 南通企业网站有哪些微信引流推广
  • 页游做的好的是哪个网站芭蕉视频app无限次数
  • 网站建设保教什么是广告营销
  • 用java建设网站百度搜索高级搜索
  • 怎么用网站做远控百度seo价格查询系统
  • 厦门网站建设培训费用长沙互联网网站建设
  • 做化验的网站个人网站seo入门
  • 网站设计的必要性广告软文小故事200字
  • 做购物网站需要什么资质可以直接打开网站的网页
  • 郑州做网站好的公司seo关键词分类
  • app开发定制公司如百度推广seo
  • 淘客做的网站属于什么类型2023北京封控了
  • seo优化网站建设哪家好企业营销策略分析论文