Java探索之Thread+IO文件的加密解密代码实例
这篇文章向大家分享了几段代码,主要是关于Thread+IO文件的加密解密,下面看看具体代码:
加密启动线程
packagecom.hz.subsection; importjava.io.File; publicclassenCodeFileThreadextendsThread{ publicFilesfiles; publicFilefile; publicFiledst; publicenCodeFileThread(Stringname,Filesfiles,Filefile,Filedst){ super(name); this.dst=dst; this.files=files; this.file=file; } publicvoidrun(){ files.enCode(file,dst); } }
解密启动线程
packagecom.hz.subsection; importjava.io.File; publicclassenCodeFileThreadextendsThread{ publicFilesfiles; publicFilefile; publicFiledst; publicenCodeFileThread(Stringname,Filesfiles,Filefile,Filedst){ super(name); this.dst=dst; this.files=files; this.file=file; } publicvoidrun(){ files.enCode(file,dst); } }
解密启动线程
packagecom.hz.subsection; importjava.io.File; publicclassdeCodeFileThreadextendsThread{ publicFilesfiles; publicFilefile; publicFiledst; publicdeCodeFileThread(Stringname,Filesfiles,Filefile,Filedst){ super(name); this.dst=dst; this.files=files; this.file=file; } publicvoidrun(){ files.deCode(dst); } }
文件对象序列化
packagecom.hz.subsection; importjava.io.ByteArrayOutputStream; importjava.io.File; importjava.io.FileInputStream; importjava.io.FileNotFoundException; importjava.io.FileOutputStream; importjava.io.IOException; importjava.io.ObjectInputStream; importjava.io.ObjectOutputStream; importjava.io.Serializable; importjava.util.ArrayList; importjava.util.List; publicclassFilesimplementsSerializable{ /** *默认序列id */ privatestaticfinallongserialVersionUID=1L; privateStringfilesNo; privateStringname; privatebyte[]content; privatebooleanflag=true; publicFiles(){ } publicFiles(StringfilesNo){} publicFiles(StringfilesNo,Stringname,byte[]content){ super(); this.name=name; this.content=content; } publicStringgetFilesNo(){ returnfilesNo; } publicvoidsetFilesNo(StringfilesNo){ this.filesNo=filesNo; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicbyte[]getContent(){ returncontent; } publicvoidsetContent(byte[]content){ this.content=content; } //加密序列化文件 publicsynchronizedvoidenCode(Filefile,Filedst){ if(!flag){ try{ wait(); }catch(InterruptedExceptione){ e.printStackTrace(); } }else{ //获取文件夹下的每一个文件 File[]chlidFiles=file.listFiles(); Listlist=newArrayList(); for(inti=0;i list){ FileOutputStreamfos=null; ObjectOutputStreamoos=null; try{ fos=newFileOutputStream(dst); oos=newObjectOutputStream(fos); oos.writeObject(list); }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); }finally{ try{ if(oos!=null){ oos.close(); oos=null; } if(fos!=null){ fos.close(); fos=null; } }catch(IOExceptione){ e.printStackTrace(); } } } publicFilesgetFiled(FiletmpFile){ Filesfiles=newFiles(); Stringname=tmpFile.getName(); files.setName(name); FileInputStreamfis=null; ByteArrayOutputStreambaos=null; try{ fis=newFileInputStream(tmpFile); baos=newByteArrayOutputStream(); byte[]buff=newbyte[1024]; inthasRead=0; while((hasRead=fis.read(buff))>-1){ baos.write(buff,0,hasRead); } files.setContent(baos.toByteArray()); returnfiles; }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); }finally{ try{ if(baos!=null){ baos.close(); baos=null; } }catch(IOExceptione){ e.printStackTrace(); } try{ if(fis!=null){ fis.close(); fis=null; } }catch(IOExceptione){ e.printStackTrace(); } } returnnull; } //解密序列化文件 publicsynchronizedvoiddeCode(Filedst){ if(!flag){ try{ wait(); }catch(InterruptedExceptione){ e.printStackTrace(); } }else{ List list=readFiles(dst); for(Filesfiles:list){ Stringname=files.getName(); byte[]content=files.getContent(); Filefile=newFile(dst.getParent()+"//bbb",name); if(!file.exists()){ try{ file.createNewFile(); }catch(IOExceptione){ e.printStackTrace(); } } FileOutputStreamfos=null; try{ fos=newFileOutputStream(file); fos.write(content); }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); }finally{ try{ if(fos!=null){ fos.close(); fos=null; } flag=false; notifyAll(); }catch(IOExceptione){ e.printStackTrace(); } } } } } privateList readFiles(Filedst){ FileInputStreamfis=null; ObjectInputStreamois=null; try{ fis=newFileInputStream(dst); ois=newObjectInputStream(fis); List list=(List )ois.readObject(); returnlist; }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); }catch(ClassNotFoundExceptione){ e.printStackTrace(); }finally{ try{ if(ois!=null){ ois.close(); ois=null; } }catch(IOExceptione){ e.printStackTrace(); } try{ if(fis!=null){ fis.close(); fis=null; } }catch(IOExceptione){ e.printStackTrace(); } } returnnull; } publicStringtoString(){ return"Files[name=" +name +",content=" +(content!=null?arrayToString(content,content.length) :null)+"]"; } privateStringarrayToString(Objectarray,intlen){ StringBufferbuffer=newStringBuffer(); buffer.append("["); for(inti=0;i 0) buffer.append(","); if(arrayinstanceofbyte[]) buffer.append(((byte[])array)[i]); } buffer.append("]"); returnbuffer.toString(); } publicinthashCode(){ returngetFilesNo().hashCode(); } publicbooleanequals(Objectobj){ if(obj!=null&&getClass()==Files.class){ Filestarget=(Files)obj; returntarget.getFilesNo().equals(filesNo); } returnfalse; } }
测试类
packagecom.hz.subsection; importjava.io.File; publicclassTestThread{ publicstaticvoidmain(String[]args){ Filesfiles=newFiles("123"); Filefile=newFile("E:\\20160928JavaBase\\Test\\aaa"); Filefile2=newFile("E:\\20160928JavaBase\\Test\\gg"); newenCodeFileThread("加密文件",files,file,newFile(file,"dst.hz")).start(); newdeCodeFileThread("解密文件",files,file,newFile(file,"dst.hz")).start(); } }
以上就是本文关于Java探索之Thread+IO文件的加密解密代码实例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:Java创建与结束线程代码示例、Java多线程之线程通信生产者消费者模式及等待唤醒机制代码详解等,有什么问题可以随时留言,小编会及时回复大家的。感谢朋友们对本站的支持!