Java原生序列化和反序列化代码实例
这篇文章主要介绍了Java原生序列化和反序列化代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
写一个Java原生的序列化和反序列化的DEMO。
需序列化的类:
packagecom.nicchagil.nativeserialize; importjava.io.Serializable; publicclassUserimplementsSerializable{ privatestaticfinallongserialVersionUID=1L; privateIntegerid; privateStringuserName; publicUser(Integerid,StringuserName){ super(); this.id=id; this.userName=userName; } publicIntegergetId(){ returnid; } publicvoidsetId(Integerid){ this.id=id; } publicStringgetUserName(){ returnuserName; } publicvoidsetUserName(StringuserName){ this.userName=userName; } publicstaticlonggetSerialversionuid(){ returnserialVersionUID; } @Override publicinthashCode(){ finalintprime=31; intresult=1; result=prime*result+((id==null)?0:id.hashCode()); result=prime*result +((userName==null)?0:userName.hashCode()); returnresult; } @Override publicbooleanequals(Objectobj){ if(this==obj) returntrue; if(obj==null) returnfalse; if(getClass()!=obj.getClass()) returnfalse; Userother=(User)obj; if(id==null){ if(other.id!=null) returnfalse; }elseif(!id.equals(other.id)) returnfalse; if(userName==null){ if(other.userName!=null) returnfalse; }elseif(!userName.equals(other.userName)) returnfalse; returntrue; } @Override publicStringtoString(){ return"User[id="+id+",userName="+userName+"]"; } }
工具类:
packagecom.nicchagil.nativeserialize; importjava.io.File; importjava.io.FileInputStream; importjava.io.FileNotFoundException; importjava.io.FileOutputStream; importjava.io.IOException; importjava.io.ObjectInputStream; importjava.io.ObjectOutputStream; importjava.io.Serializable; publicclassNativeSerializeTools{ /** *序列化 *@paramfilePath序列化的路径 *@params序列化的对象 */ publicstaticvoidwrite(StringfilePath,Serializables)throwsFileNotFoundException,IOException{ if(filePath==null||filePath.length()==0){ thrownewRuntimeException("请传入序列化路径"); } if(s==null){ thrownewRuntimeException("请传入序列化对象"); } Filef=newFile(filePath); ObjectOutputStreamoos=null; FileOutputStreamfos=null; try{ fos=newFileOutputStream(f); oos=newObjectOutputStream(fos); oos.writeObject(s); System.out.println("finish."); }finally{ if(oos!=null){ oos.close(); } if(fos!=null){ fos.close(); } System.out.println("closetheresource."); } } /** *反序列化 *@paramfilePath反序列化的路径 *@return反序列化的对象 */ publicstaticObjectread(StringfilePath)throwsClassNotFoundException,FileNotFoundException,IOException{ if(filePath==null||filePath.length()==0){ thrownewRuntimeException("请传入反序列化路径"); } Filef=newFile(filePath); ObjectInputStreamois=null; FileInputStreamfis=null; Objecto=null; try{ fis=newFileInputStream(f); ois=newObjectInputStream(fis); o=ois.readObject(); System.out.println("finish."); }finally{ if(ois!=null){ ois.close(); } if(fis!=null){ fis.close(); } System.out.println("closetheresource."); } returno; } }
测试类:
packagecom.nicchagil.nativeserialize; importjava.io.FileNotFoundException; importjava.io.IOException; importorg.junit.Assert; importorg.junit.Test; publicclassHowToUse{ privateUseruser=newUser(100,"NickHuang"); privateStringfilePath="d:/user.txt"; @Test publicvoidc1()throwsFileNotFoundException,IOException{ NativeSerializeTools.write(filePath,user); } @Test publicvoidc2()throwsFileNotFoundException,IOException,ClassNotFoundException{ Objecto=NativeSerializeTools.read(filePath); System.out.println(o); Assert.assertTrue(user.equals(o)); } }
日志:
finish.
closetheresource.
finish.
closetheresource.
User[id=100,userName=NickHuang]
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。